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
useStateanduseEffectand instead relying onreact-queryto handle preserving the state/effects. I am creating an entity withuseMutation. Something like:then I use that
generatedContractwithuseQueryandrefetchIntervalto poll for a specific status:I'd love to be able to stop the polling (have
enabledbe false) whencontractFromPollingis 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 thegenerateContractMutationfunction, thenawaitfor the data to be returned. I then set that data insetStatethis way I can then run
useQuerywith the following state in theenabled:Each time the polling returns back a result I set it as the state in a
useEffectSince
generatedContractbecomes thecontractFromPolling, whencontractFromPollingis no longerGENERATING_CONTRACTthen it will stop polling.I imagine that the
useStatesolution is the best outcome for now? Are there any tricks to be able to stop an interval fetching withreactQueryusing the data that is returned from the correspondinguseQuery? It would be awesome if there is a way where we can toggle theenabledflag through theonSuccesscallback!I made a very basic CodeSandbox showing how it's being done currently with
useStateanduseEffect: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