Allow refetch() in useQuery to Accept Query Parameters #8662
-
Currently, refetch() from useQuery only re-executes the query with the same parameters (query key and options) provided during initialization. There is no built-in way to pass different parameters directly to refetch() without changing the query key or using queryClient.fetchQuery(). Problem Statement: When using useQuery with dynamic parameters, developers often want to trigger refetch() with different parameters on demand. However, the current behavior requires either:
It would be more intuitive and convenient if refetch() could directly accept new parameters without needing to modify the query key or use an entirely different query Proposed Solution: Enable refetch() to accept query parameters and merge them with the current query key. Why This Feature Matters: Developer Experience: Simplifies triggering queries with different parameters without additional boilerplate. Efficiency: Reduces the need for state management workarounds. Environment: React Query Version: latest React Version: 18 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
https://tkdodo.eu/blog/react-query-fa-qs#how-can-i-pass-parameters-to-refetch |
Beta Was this translation helpful? Give feedback.
usually, everything that is used inside the queryFn should be part of the queryKey. it sounds like
forceRefresh
is an exception because you don’t actually want to store the result in a different key?passing a different
queryFn
is always dangerous because thequeryFn
gets stored on the query and automatic refetches might re-use those, so I would keep the same function.I would probably create a ref and pass that through
meta
: