Skip to content

fix: clear refresh timer on effects change and check it on starting new timer #853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 3, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ function useSWR<Data = any, Error = any>(
}, [key, revalidate])

useIsomorphicLayoutEffect(() => {
let timer = null
const timer = { id: null }
const tick = async () => {
if (
!stateRef.current.error &&
Expand All @@ -685,15 +685,18 @@ function useSWR<Data = any, Error = any>(
await revalidate({ dedupe: true })
}
// Read the latest refreshInterval
if (configRef.current.refreshInterval) {
timer = setTimeout(tick, configRef.current.refreshInterval)
if (configRef.current.refreshInterval && timer.id) {
timer.id = setTimeout(tick, configRef.current.refreshInterval)
}
}
if (configRef.current.refreshInterval) {
timer = setTimeout(tick, configRef.current.refreshInterval)
timer.id = setTimeout(tick, configRef.current.refreshInterval)
}
return () => {
if (timer) clearTimeout(timer)
if (timer.id) {
clearTimeout(timer.id)
timer.id = null
}
}
}, [
config.refreshInterval,
Expand Down