Reputation: 4268
I'm trying to switch from zustand to react-query as my central cache. I can get reactive query results with useQuery(queryKey, queryFn)
. However, I want to get reactive results for mutations as well. So I tried the following:
useMutation(mutationFn, {
onSuccess: (d, i) => queryClient.setQueryData(queryKey, d),
});
The problem is how to get the reactive result.
I considered useQuery(queryKey)
but the API mentions queryFn
is required unless defaults are setup. In my current use case, I don't plan on updating that queryKey
unless its via a mutation so I don't have a queryFn
. I can't use queryClient.getQueryData(queryKey)
since it's not reactive.
How can I get the mutation's reactive result?
Upvotes: 1
Views: 2609
Reputation: 4268
You can't use useQuery
without a queryFn
as it will error out.
https://codesandbox.io/s/react-query-reactive-mutation-results-txcz9?file=/src/index.js
A feature request is in progress to handle the mutations side.
https://github.com/tannerlinsley/react-query/issues/2304
Upvotes: 1