Skip to content

Commit 893730b

Browse files
jacobparisMichaelDeBoey
authored andcommitted
Update use-revalidator.md
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
1 parent 928fedc commit 893730b

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

Diff for: docs/hooks/use-revalidator.md

+23-5
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,34 @@ The state of the revalidation. Either `"idle"` or `"loading"`.
3838
Initiates a revalidation.
3939

4040
```tsx
41+
4142
function useLivePageData() {
42-
const revalidator = useRevalidator();
43-
const interval = useInterval(5000);
43+
const revalidator = useRevalidator()
44+
45+
useInterval(() => {
46+
if (revalidator.state === 'idle') {
47+
revalidator.revalidate()
48+
}
49+
}, 5000)
50+
}
4451

52+
function useInterval(callback: () => void, delay?: number) {
4553
useEffect(() => {
46-
if (revalidator.state === "idle") {
47-
revalidator.revalidate();
54+
if (delay === undefined) return
55+
56+
let id: ReturnType<typeof setTimeout>
57+
58+
function tick() {
59+
callback()
60+
id = setTimeout(tick, delay)
4861
}
49-
}, [interval, revalidator]);
62+
63+
id = setTimeout(tick, delay)
64+
65+
return () => clearTimeout(id)
66+
}, [callback, delay])
5067
}
68+
5169
```
5270

5371
## Notes

0 commit comments

Comments
 (0)