Skip to content

Instantly share code, notes, and snippets.

@kenn
Created July 21, 2023 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenn/039c95b23b9d60fa316f936e2196a765 to your computer and use it in GitHub Desktop.
Save kenn/039c95b23b9d60fa316f936e2196a765 to your computer and use it in GitHub Desktop.
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