useInterval()
The
useInterval() hook makes using setInterval inside React components declarative and safe. It solves the common "stale closure" problem where the interval callback cannot access the latest state props.Import
typescript
Usage
Counter
0
Update Interval Speed
Component.tsx
API
Arguments
callback– () => void The function to be executed every interval.delay– number | null The time in milliseconds between function calls. Passnullto pause/stop the interval.
Returns
This hook returns
void.