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

Conversation

jacobparis
Copy link
Contributor

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: #

  • Docs
  • Tests

Testing Strategy:

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
Copy link

changeset-bot bot commented Sep 16, 2024

⚠️ No Changeset found

Latest commit: 1b5d5d4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Comment on lines +52 to +68
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])
}

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

@MichaelDeBoey MichaelDeBoey changed the title Update use-revalidator.md docs(hooks/use-revalidator): update code example Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants