Manual query refetching using new query parameters #4327
-
I'm using a RQ with xstate. const profile = useGetProfile({ userId: '123'} , { enabled: false })
const [state, send] = useMachine(machine, {
services: {
async getProfile() {
const response = await profile.refetch()
return response.data
},
},
}) It works perfectly until we need to pass query parameters that are not available outside of the I'm wondering if it's possible to refetch the existing query using different query parameters. Something like this (but it doesn't work now). const response = await profile.refetch({ userId: '000'}) Is there a way to pass query parameters to the query function in an imperative, more like a mutation way? The full example would look like this (note of using const profile = useGetProfile({ userId: '123'} , { enabled: false })
const [state, send] = useMachine(machine, {
services: {
async getProfile(context, event) {
const response = await profile.refetch({ userId: event.userId })
return response.data
},
},
}) For those who are interested in using xstate and RQ together, here's another discussion statelyai/xstate#1813 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There is no such thing because queries should be cached by their parameters, and to do that, you'd have to make them part of the queryKey. So the idiomatic way to do this is described here: https://tanstack.com/query/v4/docs/guides/disabling-queries#lazy-queries I don't know how to keep the machine "in sync" with state that it doesn't really own. There is an There is even an example in there on how to use it with
I like this because it doesn't need to disable the query, so it will profit from all the automatic things that react-query does and you could use the idiomatic way to just change the queryKey when the userId changes. If you want to trigger a refetch from within the machine, you can always use one workaround I recently found is to use refs and pass them as
|
Beta Was this translation helpful? Give feedback.
There is no such thing because queries should be cached by their parameters, and to do that, you'd have to make them part of the queryKey. So the idiomatic way to do this is described here:
https://tanstack.com/query/v4/docs/guides/disabling-queries#lazy-queries
I don't know how to keep the machine "in sync" with state that it doesn't really own. There is an
input
proposal on xstate that would solve the problem nicely but I don't think it will get merged in the current way:There is even an example in there on how to use it with
useQuery
: