You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
initialStale not working, at least not always #1007
I have a currentResult before any fetch has been made and only initialData has loaded.
So above statement is not true and initialStale value is never taken into account.
That's what I believe without any deep knowledge of the react-query code.
This is what currentResult looks like for me when reaching that point without any fetch and then never running a fetch even though initialStale is set to true:
@boschni
Must be, there must be some change between 2.5.14 and 2.17.0 that alters the behavior because before upgrading initialStale true worked fine, now I have to wait (since I don't always have staleTime set to 0 in query config).
If I load the page, leave the window and wait for the staletime to expire and go back (trigger a refetch with window focus) the data is loaded and page propagated.
Now, I don't know why I have a currentResult at that stage if I'm not supposed to have one,
but from the data in the currentResult I think it should anyway figure out that the conditions in the if statement should be true and initialStale logic and everything should be run?
One could probably fix it by just changing the if statement to something like if ((!currentResult || currentResult.isInitialData) && state.isInitialData) {
or something, but I know too little about the source to know if that's a fix or just a bad workaround for my particular problem.
@boschni Seems like it works! At least for me :)
It now fetches on first page load when initialStale and staleTime has been set.
By the way. What is the best/easiest way to test a react-query pull request?
Tried pulling, build and link but got the Error: Invalid hook call... (more than one react error)
So resorted to yarn add https://github.com/boschni/react-query.git#feature/keep-initial-stale and then:
yarn, yarn build, yarn build:types
and then remove the node_modules directory in the react-query folder to get it running.
Activity
boschni commentedon Sep 13, 2020
We do have a test which should cover this: https://github.com/tannerlinsley/react-query/blob/master/src/react/tests/useQuery.test.tsx#L1209 . Is your case different?
emanuel-lundman commentedon Sep 13, 2020
@boschni
Must be, there must be some change between 2.5.14 and 2.17.0 that alters the behavior because before upgrading initialStale true worked fine, now I have to wait (since I don't always have staleTime set to 0 in query config).
If I load the page, leave the window and wait for the staletime to expire and go back (trigger a refetch with window focus) the data is loaded and page propagated.
The test checks for the following QueryResults
I get this
QueryResult:And from what I gather from browser devtools it happens because I've got a
currentResultobject when reachinghttps://github.com/tannerlinsley/react-query/blob/c7e342a91a79ea9d005ffa93e491a6c879a1a7e8/src/core/queryObserver.ts#L232
So the initialStale logic is never run because execution goes directly to:
https://github.com/tannerlinsley/react-query/blob/c7e342a91a79ea9d005ffa93e491a6c879a1a7e8/src/core/queryObserver.ts#L241
Now, I don't know why I have a currentResult at that stage if I'm not supposed to have one,
but from the data in the currentResult I think it should anyway figure out that the conditions in the if statement should be true and initialStale logic and everything should be run?
(copied from original issue above)
currentResult{ "status": "success", "isLoading": false, "isSuccess": true, "isError": false, "isIdle": false, "data": [], "error": null, "failureCount": 0, "isFetched": false, "isFetchedAfterMount": false, "isFetching": false, "isFetchingMore": false, "isInitialData": true, "isPreviousData": false, "isStale": true, "updatedAt": 1599993452356 }One could probably fix it by just changing the if statement to something like
if ((!currentResult || currentResult.isInitialData) && state.isInitialData) {or something, but I know too little about the source to know if that's a fix or just a bad workaround for my particular problem.
emanuel-lundman commentedon Sep 13, 2020
@boschni about the test, if you add
staleTime: 60_000to the test https://github.com/tannerlinsley/react-query/blob/master/src/react/tests/useQuery.test.tsx#L1209The test fails. initialStale=true with a staleTime seems not to be working. Should still be stale before first fetch even though it has a stale time.
Haven't had time yet to check how to fix it without breaking anything else though.
boschni commentedon Sep 13, 2020
Are you able to test if this PR fixes it? #1010
emanuel-lundman commentedon Sep 13, 2020
@boschni Seems like it works! At least for me :)
It now fetches on first page load when initialStale and staleTime has been set.
By the way. What is the best/easiest way to test a react-query pull request?
Tried pulling, build and link but got the
Error: Invalid hook call...(more than one react error)So resorted to
yarn add https://github.com/boschni/react-query.git#feature/keep-initial-staleand then:yarn, yarn build, yarn build:types
and then remove the node_modules directory in the react-query folder to get it running.
emanuel-lundman commentedon Sep 14, 2020
Fixed and working in: 460ab33