diff --git a/docs/hooks/use-revalidator.md b/docs/hooks/use-revalidator.md index b78561b9f96..4fb370b1b19 100644 --- a/docs/hooks/use-revalidator.md +++ b/docs/hooks/use-revalidator.md @@ -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 + + function tick() { + callback() + id = setTimeout(tick, delay) } - }, [interval, revalidator]); + + id = setTimeout(tick, delay) + + return () => clearTimeout(id) + }, [callback, delay]) } + ``` ## Notes