Cannot stop polling by setting refreshInterval 0
See original GitHub issueSWR version 0.3.1
I have a demo reproducing this issue here.
In this demo I have a flag variable controlling whether swr should poll by conditionally setting refreshInterval
value.
{
refreshInterval: shouldPoll ? 1000 : 0
}
In my swr config I also have an onSuccess
callback in which I change my flag value
{
refreshInterval: shouldPoll ? 1000 : 0,
onSuccess() {
setShouldPoll(false);
}
}
When the flag is set to false
as the first fetch finishes, the polling stops normally as expected. Everything is fine so far.
However, if I set the flag value in the callback not of the first fetch but the revalidation fetches, polling is not stopping.
{
refreshInterval: intValue < 2 ? 1000 : 0, // intValue is initially 0
onSuccess() {
setIntValue(value => value + 1);
}
}
After the first fetch, intValue is set to 1, and polling is supposed to continue. After the 2nd fetch, intValue is set to 2, and polling is supposed to stop because refreshInterval is now 0. But the polling continued.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
Next.js - SWR hook question about dedupeInterval and ...
Yes, if the user opens your page and does nothing but reading content during 20 seconds, and you have set the refreshInterval to...
Read more >Change the period override setting or refresh interval for the ...
The Period override always reverts to Auto when the dashboard is closed or the browser is refreshed. Different settings for Period override can't...
Read more >Settings for minimizing periodic WAN traffic - Windows Client
The background retry quit time. When a program runs a periodic search for domain controllers and cannot find a domain controller, the value...
Read more >API - SWR
Disabled by default: refreshInterval = 0; If set to a number, polling interval in milliseconds; If set to a function, the function will...
Read more >Queries - Apollo GraphQL Docs
Note that if you set pollInterval to 0 , the query does not poll. You can also start and stop polling dynamically with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think it’s the naming of
onSuccess
options that is tricky. I understand it as ‘when fetches finish’, while it’s actually not.A workaround can be moving my ‘when fetches finish’ logic from
onSuccess
into a customer fetcher.A simplified revalidation steps
newData = await CONCURRENT_PROMISES[key]
the fetcher has finished and we have newData
eventsRef.current.emit('onSuccess', newData, key, config)
onSucuess calledSo
onSuccess
is called after we successfully get the newData.