Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep isSuccess consistent when refetching after an error #4732

Merged
merged 1 commit into from
Nov 28, 2024

Conversation

markerikson
Copy link
Collaborator

@markerikson markerikson commented Nov 24, 2024

This PR:

  • Makes a further tweak to the logic for calculating isSuccess, such that we don't flip from false to true temporarily when refetching after an error

Fixes #4240

Investigation

Per #4240, if you have a sequence where:

  • The initial fetch succeeds
  • The first refetch fails
  • You run a second refetch that also fails

the observed behavior is:

      [
        // Initial renders
        { id: 1, isFetching: true, isSuccess: false, isError: false },
        { id: 1, isFetching: true, isSuccess: false, isError: false },
        // Data is returned
        { id: 1, isFetching: false, isSuccess: true, isError: false },
        // Started first refetch
        { id: 1, isFetching: true, isSuccess: true, isError: false },
        // First refetch errored
        { id: 1, isFetching: false, isSuccess: false, isError: true },
        // Started second refetch <-------- `isSuccess` flips true here!
        { id: 1, isFetching: true, isSuccess: true, isError: false },
        // Second refetch errored
        { id: 1, isFetching: false, isSuccess: false, isError: true },
      ]

The issue is that isSuccess was false when the first refetch settled, but then flips to true when we start the second refetch. This does seem unintuitive and buggy.

Similar to #4731 , I think the logic for calculating isSuccess isn't sufficient. In this case:

  • We know the current cache entry is not in a success state because it's fetching
  • we are fetching and we do technically have cached data...
  • but we know that the last request actually failed

Given that, I think the right thing to do is include && !lastResult?.isError, leaving us with:

    const isSuccess =
      currentState.isSuccess ||
      (hasData &&
        ((isFetching && !lastResult?.isError) || currentState.isUninitialized))

I see that flip the second refetch case to stay with isSuccess: false as I'd hope.

Copy link

codesandbox bot commented Nov 24, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

netlify bot commented Nov 24, 2024

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit 311f31c
🔍 Latest deploy log https://app.netlify.com/sites/redux-starter-kit-docs/deploys/674277d99b5e7800089334db
😎 Deploy Preview https://deploy-preview-4732--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 311f31c:

Sandbox Source
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration
rtk-esm-cra Configuration

Copy link

size-limit report 📦

Path Size
1. entry point: @reduxjs/toolkit/query/react (cjs, production.min.cjs) 24.45 KB (-0.34% 🔽)
2. entry point: @reduxjs/toolkit/query/react (without dependencies) (cjs, production.min.cjs) 2.87 KB (+0.28% 🔺)
3. createApi (react) (.modern.mjs) 15.66 KB (+0.06% 🔺)

@markerikson markerikson merged commit ee22bef into master Nov 28, 2024
74 checks passed
@markerikson markerikson deleted the bugfix/4240-isSuccess branch November 28, 2024 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected behaviour of isSuccess on second refetch
1 participant