Created
July 21, 2023 17:18
-
-
Save kenn/039c95b23b9d60fa316f936e2196a765 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRevalidator } from '@remix-run/react' | |
import { useEffect } from 'react' | |
// Rendering will ensure all page loaders are revalidated according to a given | |
// frequency | |
export const Poller = ({ seconds }) => { | |
usePolling(seconds) | |
return null | |
} | |
const usePolling = (seconds: number) => { | |
const revalidator = useRevalidator() | |
useEffect(() => { | |
const interval = setInterval(() => { | |
if (revalidator.state === 'idle') { | |
console.log('Revalidating page...') | |
revalidator.revalidate() | |
} | |
}, seconds * 1000) | |
return () => clearInterval(interval) | |
}, [revalidator, seconds]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment