DEV Community

thuan1412
thuan1412

Posted on

Callback function that use the state of useState() hook

Callback function that use state

The callback funtion of setInterval, setTimeout and eventListener only get the initialized value of useState in React.

You can see the demo here. The value is log by the callback of setInterval alway be zero.

We can use useRef to solve this problem. See more solution on this

Interesting tutorial about React hooks: https://github.com/ohansemmanuel/react-hooks-cheatsheet

Top comments (0)