-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
docs(hooks/use-revalidator): update code example #9992
base: main
Are you sure you want to change the base?
Conversation
The original example looks like a GPT solution, the interval was never accounted for and the useEffect would just keep running immediately as soon as the revalidation completes
|
function useInterval(callback: () => void, delay?: number) { | ||
useEffect(() => { | ||
if (revalidator.state === "idle") { | ||
revalidator.revalidate(); | ||
if (delay === undefined) return | ||
|
||
let id: ReturnType<typeof setTimeout> | ||
|
||
function tick() { | ||
callback() | ||
id = setTimeout(tick, delay) | ||
} | ||
}, [interval, revalidator]); | ||
|
||
id = setTimeout(tick, delay) | ||
|
||
return () => clearTimeout(id) | ||
}, [callback, delay]) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the idea behind useInterval
was to hide the details around the setTimeout
/useEffect
so the docs would be focused on useRevalidator
- but looks like we simplified it down too much such that it had a bug. Do you think we need to include the implementation of useInterval
in your corrected version? Or do you think folks can grok the usage of useRevalidator
without it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just link to Dan Abramov's blog post and call it a day https://overreacted.io/making-setinterval-declarative-with-react-hooks/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's very little content on this page so I don't see a pressing need to minimize it, but if you want to drop the implementation then that's fine with me
I just like being able to go to docs and copy/paste things that work, and having the useInterval there to also copy makes that easier
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I like linking out to an implementation of it better than bloating the code block with unrelated lines personally
The original example looks like a GPT solution, the interval was never accounted for and the useEffect would just keep running immediately as soon as the revalidation completes
Closes: #
Testing Strategy: