T
TanStack3y ago
xenial-black

Pass different parameter to query fn depending on how it is used

Hello all, I have the following case: I have a react native app that needs to make a network request the first time the app opens and every time the app comes from background to foreground. It is also invalidated at times throughout the lifetime of the app. Here is the query:
export const useHomeQuery = () =>
useQuery([endpoints.INITIALIZATION], getHomeData, {
staleTime: Infinity,
});
export const useHomeQuery = () =>
useQuery([endpoints.INITIALIZATION], getHomeData, {
staleTime: Infinity,
});
Here is how I refetch when the app comes from background to foreground:
useActionOnWakeUp(refetch);
useActionOnWakeUp(refetch);
and throughout the app I invalidate the query according to some requirements:
queryClient.invalidateQueries([endpoints.INITIALIZATION]);
queryClient.invalidateQueries([endpoints.INITIALIZATION]);
Here is what I need to do. I need to be able to pass a value to the query that is sent with the request to tell the server why this query was called. Let's say it is an attribute called purpose and has values of either first open | reopen | invalidating Basically I need a way to know which one cause the query to run. How can I achieve that?
2 Replies
ratty-blush
ratty-blush3y ago
nothing built-in to do this. You can write the reason to a ref / global var / store and read it from the queryFn
xenial-black
xenial-blackOP3y ago
Alright thanks for the answer!

Did you find this page helpful?