-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Why useEffect triggers twice after the API response is cached? #5538
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
Comments
Isn't the behavior normal since it is about new data coming from API ? I mean, I inspected the stack traces and found that the
|
while this is true, react-query does structural sharing, and it does that also on the result of select. So if it's deeply the same, the same object reference should be reused. |
I'm not too familiar with the internals, but I think the issue is with https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L470, where you call I added a |
I think I know what's happening in there. I will followup with a PR and explanations later today. |
Following up with my PR. Issue explanations
First, let's observe both stack traces and see from where the selector is called: // first time:
selector Error
at Object.selector [as select] (https://h34qgm.csb.app/src/App.js:48:27)
at QueryObserver.createResult (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/queryObserver.js:294:26)
at QueryObserver.getOptimisticResult (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/queryObserver.js:109:17)
at Object.useBaseQuery (https://h34qgm.csb.app/node_modules/@tanstack/react-query/build/lib/useBaseQuery.js:61:27)
at useQuery (https://h34qgm.csb.app/node_modules/@tanstack/react-query/build/lib/useQuery.js:11:23)
at App (https://h34qgm.csb.app/src/App.js:18:32)
// second time:
index.js:27 selector Error
at Object.selector [as select] (https://h34qgm.csb.app/src/App.js:48:27)
at QueryObserver.createResult (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/queryObserver.js:294:26)
at QueryObserver.updateResult (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/queryObserver.js:383:29)
at QueryObserver.onQueryUpdate (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/queryObserver.js:459:10)
at eval (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/query.js:445:18)
at Array.forEach (<anonymous>)
at eval (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/query.js:444:22)
at Object.batch (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/notifyManager.js:24:16)
at Query.dispatch (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/query.js:443:33)
at Query.fetch (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/query.js:275:12)
at QueryObserver.executeFetch (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/queryObserver.js:172:37)
at QueryObserver.setOptions (https://h34qgm.csb.app/node_modules/@tanstack/query-core/build/lib/queryObserver.js:90:12)
at eval (https://h34qgm.csb.app/node_modules/@tanstack/react-query/build/lib/useBaseQuery.js:66:14)
at commitHookEffectListMount (https://h34qgm.csb.app/node_modules/react-dom/cjs/react-dom.development.js:23150:26)
The second call is started from the
So, the culprit is
The deep object cloning occurs between SolutionTo solve this, the minimal impactful solution I found is to assign the observer's For more information, take a look at my PR, that adds a regression test for this specific use case. ConsiderationsI am not a react-query pro, so my knowledge on the API is minimal, which may lead my fix to cause other unexpected regressions:
Hopefully this helps. |
Looks like this is still a issue in the latest v4 when It is fixed in v5 tho even with |
Discussed in #5537
Originally posted by dev-rish June 5, 2023
Describe the Bug
After switching between 1 & 2 for couple of times (so that react query caches data for each of them), the useEffect still triggers twice.
The expected behavior is seen till v3.8.3 i.e. useEffect only triggers once. Once react query is updated to v3.9.0 every click on 1 or 2 causes useEffect to trigger twice.
Is there a way to prevent the second trigger?
I found related issues but still wanted some more info. if possible.
Minimal, reproducible example
v3: https://codesandbox.io/s/optimistic-firefly-1tk7i5
v4: https://codesandbox.io/s/sad-scott-cidk6s?file=/src/App.js
Steps to reproduce
Click in 1 > Click on 2 > Click in 1 > Click on 2
Expected behavior
useEffect to trigger only once when
id
is switched.How often does this bug happen?
Always
Note:
select
related, because it works as expected ifselect
is removedThe text was updated successfully, but these errors were encountered: