How to initiate search params from initial api response without triggering re-fetch?
1. I fetch initial data using
useQuery
and no search params that are passed down as args and part of queryKey
2. Endpoint returns initial parameters.
3. I add these initial parameters as search params to the URL
4. I retrieve these params via useSearch
from the router to show UI based on it it
5. Since search params also feed the useQuery
and now that params exist, queryKey
s changed, and data is fetched again
6. Since the endpoint returns the same data with these search params as with no params at all, this is unnecessary.
How to solely rely on search params as your state manager while preventing a re-trigger of the query because setting initial search params changed query keys?
References:
- https://discord.com/channels/719702312431386674/12202656368702300587 Replies
absent-sapphire•2y ago
What’s your issue with how it works currently?
fair-roseOP•2y ago
The problem I face is that the endpoint without any search params returns the same data as with default search params attached. But since I need search params in the address bar to be attached the whole time to use them in the UI, I want to avoid that useQuery is triggered again with default search params returning the exact same data as with no params at all
other-emerald•2y ago
use
initialData
to initialize the query with default search params attached from the one without search paramsfair-roseOP•2y ago
Thanks for the response. But this would also mean that I know the search params upfront right? In fact, they can differ per user logged in
other-emerald•2y ago
why would you need to know them upfront?
fair-roseOP•2y ago
ok maybe I got you wrong. You suggest:
1. Query initial data from the backend without any search params
2. Take the response data and set them as
initialData
.
3. Once, queryKeys update because of the default search params set now, a query won't be triggered again, because we set initialData
from the very first query response
Is this right?other-emerald•2y ago
yeah i guess