Skip to content

Fix unstable selector reference inside of useQueryState and isSuccess during refetch-after-error - #5456

Merged
markerikson merged 3 commits into
masterfrom
fix/rtkq-hooks-stable-selector
Sep 29, 2026
Merged

markerikson merged 3 commits into
masterfrom
fix/rtkq-hooks-stable-selector

Conversation

@markerikson

Copy link
Copy Markdown
Collaborator

Found this one while doing some compat testing with the R-R useSignalSelector PR 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 icky store.getState()-in-render read, and I have a suspicion this might fix one or two of the transient-isSuccess-flicker issues too.

Problem

useQueryState passed a fresh inline closure to useSelector on every render:

useSelector((state) => querySelector(state, lastValue.current), shallowEqual)

The closure exists to feed lastValue.current (the previous default result) into queryStatePreSelector, which keeps stale data visible while a new arg loads. But a new selector identity makes React-Redux re-run the selector on every render, and the hook also read store.getState() directly in render to compute the next lastValue.

That re-run exposed a bug: queryStatePreSelector is not idempotent. After a successful fetch, a failed refetch, and then a refetch that is still pending, the cache entry holds both data and error. The first pass computes isSuccess: false (via lastResult.isError). A second pass over the same state, fed the first pass's result as lastResult, flips it to true. Any unrelated parent re-render triggered that second pass.

Changes

  • Memoize the selector on [selectDefaultResult, selectFromResult]. The closure still reads lastValue.current at call time, so behavior on arg changes and resetApiState is unchanged. The default result for the next lastValue is captured from inside the memoized selector (latestDefaultResult ref) and committed in the layout effect, which removes the separate raw store.getState() read in render. useStore is no longer destructured from the hooks module (the module option itself is unchanged).
  • Derive isSuccess from the cache entry in both queryStatePreSelector and infiniteQueryStatePreSelector: currentState.error === undefined instead of !lastResult?.isError. The slice keeps error on 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.
  • Tests (useQueryStateLastResult.test.tsx): isSuccess stays false across unrelated re-renders during refetch-after-error, with and without selectFromResult; previous data is kept while a new arg loads and switching back to a cached arg is immediate; resetApiState resets a mounted hook. The two isSuccess tests 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: false and now reports true. The entry for B has data and no error, so the new value matches what the flag means.

- 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.
@codesandbox

codesandbox Bot commented Sep 29, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web Editor • VS Code • Insiders

Open Preview

@github-actions

Copy link
Copy Markdown

size-limit report 📦

Path Size
1. entry point: @reduxjs/toolkit/query/react (browser.mjs) 19.07 KB (+0.05% 🔺)
2. entry point: @reduxjs/toolkit/query/react (without dependencies) (browser.mjs) 2.48 KB (+0.4% 🔺)
3. createApi (react) (.modern.mjs) 18.58 KB (-0.18% 🔽)

@pkg-pr-new

pkg-pr-new Bot commented Sep 29, 2026

Copy link
Copy Markdown

Open in StackBlitz

@reduxjs/rtk-codemods

npm i https://pkg.pr.new/@reduxjs/rtk-codemods@421e613 -D
yarn add https://pkg.pr.new/@reduxjs/rtk-codemods@421e613.tgz -D
pnpm add https://pkg.pr.new/@reduxjs/rtk-codemods@421e613.tgz -D
bun add https://pkg.pr.new/@reduxjs/rtk-codemods@421e613.tgz -D

@rtk-query/codegen-openapi

npm i https://pkg.pr.new/@rtk-query/codegen-openapi@421e613 -D
yarn add https://pkg.pr.new/@rtk-query/codegen-openapi@421e613.tgz -D
pnpm add https://pkg.pr.new/@rtk-query/codegen-openapi@421e613.tgz -D
bun add https://pkg.pr.new/@rtk-query/codegen-openapi@421e613.tgz -D

@rtk-query/graphql-request-base-query

npm i https://pkg.pr.new/@rtk-query/graphql-request-base-query@421e613 -D
yarn add https://pkg.pr.new/@rtk-query/graphql-request-base-query@421e613.tgz -D
pnpm add https://pkg.pr.new/@rtk-query/graphql-request-base-query@421e613.tgz -D
bun add https://pkg.pr.new/@rtk-query/graphql-request-base-query@421e613.tgz -D

@reduxjs/toolkit

npm i https://pkg.pr.new/@reduxjs/toolkit@421e613 -D
yarn add https://pkg.pr.new/@reduxjs/toolkit@421e613.tgz -D
pnpm add https://pkg.pr.new/@reduxjs/toolkit@421e613.tgz -D
bun add https://pkg.pr.new/@reduxjs/toolkit@421e613.tgz -D

commit: 421e613

@netlify

netlify Bot commented Sep 29, 2026

Copy link
Copy Markdown

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

Name Link
🔨 Latest commit 421e613
🔍 Latest deploy log https://app.netlify.com/projects/redux-starter-kit-docs/deploys/6abb1047a797bd000859caa2
😎 Deploy Preview https://deploy-preview-5456--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

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

@markerikson
markerikson merged commit 483f0fb into master Sep 29, 2026
52 checks passed
@aryaemami59
aryaemami59 deleted the fix/rtkq-hooks-stable-selector branch October 28, 2026 13:25
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.

1 participant