While most paradigms don’t require React developers to know much about what is going on under the hood, working with intervals can become a bit tricky. The goal of this article is to walk you through all the ways you can work with intervals and state in react.
What makes intervals a bit tricky in React is that the callback function you pass to them will inevitably close (remember closures?) over the current state of your component. Your interval will see “old” state and not work the way you intended. This is often confusing for new React developers.
Let’s look at a naive implementation of a counter that increments a number every second using setInterval
#react #javascript