-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useQuery is not reactive to its queryClient parameter #8256
Comments
yeah I don’t think we really support swapping out the queryClient on the fly. What would be a use-case for that? |
I understand how swapping out the queryClient can look odd. This is certainly an advanced use case. The short answer is that our queryClient is tied to an external "store" instance, like so: function createQueryClient(store) {
const queryCache = new QueryCache({
onSuccess(data) {
// logic that puts data in `store`
},
});
return new QueryClient({ queryCache });
}
Also, our However, since Tricks like Again, I am aware this must appear as an unusual use case. Let me know if you need any more information. |
@TkDodo would it be OK if I created a PR for this? The PR would fix usages of ref and state that assume Before const [observer] = React.useState(
() => new Observer(client, defaultedOptions),
) After Following React recommendation to re-set the state during render: function useObserver(client, options) {
const [previousClient, setPreviousClient] = React.useState(client)
const [observer, setObserver] = React.useState(
() => new Observer(client, options)
);
if (client !== previousClient) {
setPreviousClient(client)
const newObserver = new Observer(client, options)
setObserver(newObserver)
return newObserver
}
return observer
}
|
I don’t think it’s that easy because this doesn’t unsubscribe the previous observer, so it will still stick around with a reference to the old |
Describe the bug
When calling
useQuery(opts, queryClient)
, the hook will (correctly) use the providedqueryClient
argument as the query client.However, when calling the hook again with a different QueryClient instance, the hook will keep using the initial query client instance.
Your minimal, reproducible example
https://codesandbox.io/p/sandbox/boring-ellis-rk8smt?workspaceId=1f057848-e245-4f2a-94be-0866c95684db
Steps to reproduce
See the codesanbox for a reproducing example.
In a nutshell, it boils down to
Expected behavior
I expected
useQuery
to be reactive to changes in itsqueryClient
parameter.How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Tanstack Query adapter
None
TanStack Query version
v5.55.0
TypeScript version
v5.1.6
Additional context
As a workaround, we are setting a
key
prop to the React component that callsuseQuery
. The key is updated whenever thequeryClient
parameter changes. This makesuseQuery
reset and use the new query client.This workaround is not ideal for us, because it forces us to "lift the state up", breaking the abstraction of the component.
The text was updated successfully, but these errors were encountered: