Repository navigation
Fix unstable selector reference inside of useQueryState and isSuccess during refetch-after-error - #5456
Merged
Conversation
- Give useSelector a stable selector so it only re-runs on store changes or when the endpoint/arg/selectFromResult actually change - Capture the un-narrowed default result from inside the selector instead of re-selecting from store.getState() during render
…error The pre-selectors read lastResult.isError to decide whether a refetch with stale data counts as a success. After one pass lastResult already reflects the pending status, so a second selection with the same state flipped isSuccess to true. The entry keeps error set until the next success, so reading it directly is idempotent.
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
size-limit report 📦
|
@reduxjs/rtk-codemods
@rtk-query/codegen-openapi
@rtk-query/graphql-request-base-query
@reduxjs/toolkit
commit: |
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
This was referenced Nov 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found this one while doing some compat testing with the R-R
useSignalSelectorPR and RTKQ. Seems like we've had this inline selector definition being passed in since the early alphas in 2020 and we really didn't need to do that. Fixing this also conveniently removes the ickystore.getState()-in-render read, and I have a suspicion this might fix one or two of the transient-isSuccess-flicker issues too.Problem
useQueryStatepassed a fresh inline closure touseSelectoron every render:The closure exists to feed
lastValue.current(the previous default result) intoqueryStatePreSelector, which keeps staledatavisible while a new arg loads. But a new selector identity makes React-Redux re-run the selector on every render, and the hook also readstore.getState()directly in render to compute the nextlastValue.That re-run exposed a bug:
queryStatePreSelectoris not idempotent. After a successful fetch, a failed refetch, and then a refetch that is still pending, the cache entry holds bothdataanderror. The first pass computesisSuccess: false(vialastResult.isError). A second pass over the same state, fed the first pass's result aslastResult, flips it totrue. Any unrelated parent re-render triggered that second pass.Changes
[selectDefaultResult, selectFromResult]. The closure still readslastValue.currentat call time, so behavior on arg changes andresetApiStateis unchanged. The default result for the nextlastValueis captured from inside the memoized selector (latestDefaultResultref) and committed in the layout effect, which removes the separate rawstore.getState()read in render.useStoreis no longer destructured from the hooks module (the module option itself is unchanged).isSuccessfrom the cache entry in bothqueryStatePreSelectorandinfiniteQueryStatePreSelector:currentState.error === undefinedinstead of!lastResult?.isError. The slice keepserroron the entry through a pending refetch and only clears it on fulfillment, so this is the same signal without depending on the previous pass. This also makes the pre-selector idempotent.useQueryStateLastResult.test.tsx):isSuccessstaysfalseacross unrelated re-renders during refetch-after-error, with and withoutselectFromResult; previous data is kept while a new arg loads and switching back to a cached arg is immediate;resetApiStateresets a mounted hook. The twoisSuccesstests fail on master.Effects
Stock React-Redux: no selector call on unrelated renders; one fewer raw state read per render. Otherwise identical call counts.
With the experimental signals-based
useSelector, where a new selector identity forces a full tracked re-evaluation, selector evaluations per dispatch in the RTKQ benchmark drop ~30%.Known edge: switching from an errored arg A (with stale data) to an arg B already pending elsewhere previously reported
isSuccess: falseand now reportstrue. The entry for B has data and no error, so the new value matches what the flag means.