This is a series of tying out the custom hooks introduced in the really good article by team about useful React custom hooks.
useInterval
Counts up by 2 at 2.5-second intervals
This is a series of tying out the custom hooks introduced in the really good article by team about useful React custom hooks.
Counts up by 2 at 2.5-second intervals
For further actions, you may consider blocking this person and/or reporting abuse
Ramu Narasinga -
Robrecht Meersman -
Ankita -
Andrey Polischuk -
Top comments (1)
At first I was wondering why we need to use
useRef()
in theuseInterval
hook.And I found the explanation by Dan Abramov in his blog Making setInterval Declarative with React Hooks — Overreacted
Problems without
useRef()
Solution
We don’t replace the interval at all.
Instead, introduced a mutable
savedCallback
variable pointing to the latest interval callback.This mutable savedCallback needs to “persist” across the re-renders.
=>
useRef()