October 2019
Intermediate to advanced
426 pages
11h 49m
English
The react-hookedup library also provides Hooks for setInterval and setTimeout. These work similarly to calling setTimeout or setInterval directly, but as a React Hook, which will persist between re-renders. If we directly defined timers in our function component without Hooks, we would be resetting the timer every time the component re-renders.
We can pass the time in milliseconds as a second argument. We can use them as follows:
import { useInterval, useTimeout } from 'react-hookedup'useInterval(() => { ... }, 1000)useTimeout(() => { ... }, 1000)
As we can see, Hooks greatly simplify how we deal with intervals and timeouts in React.
Read now
Unlock full access