T
TanStack3y ago
absent-sapphire

Why is refetch not being called?

I have a case when a query is made and then in a useEffect I get the user's location and use refetch to make the request again. Depending on the user's device, getting the location may be very fast, less than 1 second, and then react query is dropping is dropping the refetch. I guess it is dropping it because not enough time passed from the first request? How can I tell react-query: trust me. I know what I am doing and make the request regardless how much time passed. I also want stale time to be infinity. Here is a code sandbox: https://codesandbox.io/s/cranky-dhawan-uen199?file=/src/App.js:0-669
itsramiel
CodeSandbox
cranky-dhawan-uen199 - CodeSandbox
cranky-dhawan-uen199 by itsramiel using react, react-dom, react-query, react-scripts
3 Replies
metropolitan-bronze
metropolitan-bronze3y ago
Hi, your useEffect would be called only if refetch changes. This is not the case: it remains the same as long as the query lives. So you would get one call to refetch after the first render and that's it. I'm not sure to fully understand your use-case. Is your query the one providing you the user's location? Or do you acquire user's location and want to call your query as soon as you have a location?
absent-sapphire
absent-sapphireOP3y ago
Yeah I know the refetch should only be called once, but if you check the logs, there is only one 'calling', when I want it to be two because the useQuery makes the request and I refetch again in the useEffect. My use case is the second one. I make the query and then in a useEffect I get the user location and then make the query again, but because the first query is in flight, the second one is dropped. I do not want to drop the second one. I also do not want to put the user location as query key because I want to make the request after I check the location, regardless if it changes or not. If there is anything not clear let me know.
metropolitan-bronze
metropolitan-bronze3y ago
But if you put the location in the querykey, you would have: - one call (the first) without a location while location is being retrieved - another call with the location once there About "because the useQuery makes the request and I refetch again in the useEffect.", you don't have any control on when your useEffect is called. And for sure it would be called only once since refetch won't change once query is created.

Did you find this page helpful?