react-use-polling is a react hooks library for polling asynchronous processes.
npm install react-use-pollingimport { useState } from 'react'
import { usePolling } from 'react-use-polling'
function App() {
const [count, setCount] = useState<number>(0)
const updateCountAsync = async () => {
await new Promise(r => setTimeout(r, 100))
setCount(c => c + 1)
}
const { pause } = usePolling(updateCountAsync, 1000)
return (
<div>
<span>{count}</span>
<button onClick={() => pause()}>Pause Polling</button>
</div>
)
}This hook uses requestAnimationFrame.
It stops processing in the background to improving performance and reducing battery consumption.
This hook uses setInterval.
This will continue the polling process even in the background.
Kohei Oyama (@hey3)