Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
28 changes: 23 additions & 5 deletions docs/hooks/use-revalidator.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,34 @@ The state of the revalidation. Either `"idle"` or `"loading"`.
Initiates a revalidation.

```tsx

function useLivePageData() {
const revalidator = useRevalidator();
const interval = useInterval(5000);
const revalidator = useRevalidator()

useInterval(() => {
if (revalidator.state === 'idle') {
revalidator.revalidate()
}
}, 5000)
}

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])
}

Comment on lines +52 to +68
Copy link
Contributor

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?

Copy link
Contributor

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/

Copy link
Contributor Author

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

Copy link
Contributor

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

```

## Notes
Expand Down