Replies: 3 comments 7 replies
-
try something like this const [refetchInterval, setRefetchInterval] = React.useState(2000)
const { data, status } = useQuery({
queryKey: ["poll"],
queryFn: fakePoll,
config: {
refetchInterval,
enabled: contract?.status === "Processing",
onSuccess: (result) => setRefetchInterval(0)
}
}); |
Beta Was this translation helpful? Give feedback.
3 replies
-
Unfortunately, although this approach worked in July, from updating to version |
Beta Was this translation helpful? Give feedback.
2 replies
-
This is now possible since 3.25.0 by providing a function directly to refetchInterval |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a way to stop the interval based off the returned response?
I'm trying to be clever with not using
useState
anduseEffect
and instead relying onreact-query
to handle preserving the state/effects. I am creating an entity withuseMutation
. Something like:then I use that
generatedContract
withuseQuery
andrefetchInterval
to poll for a specific status:I'd love to be able to stop the polling (have
enabled
be false) whencontractFromPolling
is a specific status, otherwise it will just go on forever (or until the user closes the modal).A solution I do have includes using
useState
. I instead call thegenerateContractMutation
function, thenawait
for the data to be returned. I then set that data insetState
this way I can then run
useQuery
with the following state in theenabled
:Each time the polling returns back a result I set it as the state in a
useEffect
Since
generatedContract
becomes thecontractFromPolling
, whencontractFromPolling
is no longerGENERATING_CONTRACT
then it will stop polling.I imagine that the
useState
solution is the best outcome for now? Are there any tricks to be able to stop an interval fetching withreactQuery
using the data that is returned from the correspondinguseQuery
? It would be awesome if there is a way where we can toggle theenabled
flag through theonSuccess
callback!I made a very basic CodeSandbox showing how it's being done currently with
useState
anduseEffect
:https://codesandbox.io/s/react-query-stop-polling-usestateuseeffect-vz4vh
and here is another showing the React Query only implementation that I'm curious on if it's possible or not:
https://codesandbox.io/s/react-query-polling-no-stateeffect-q4q52
I actually ended up doing some more tinkering and found one solution (also made the queries a tad more complex to match closer to what I am originally doing).
https://codesandbox.io/s/one-way-react-query-polling-no-stateeffect-e2hyf?file=/src/App.js
by removing the old query first, I'm able to set a new one with the data that was originally retrieved from the first query while also turning off the interval. This seems to work great for my use case, but I'm not 100% sure if this is ideal or not.
Beta Was this translation helpful? Give feedback.
All reactions