Reputation: 33
I have a number variable in my react project that changes every couple of minutes.
I would like to make a popup on the screen that displays whether or not the number increased or decreased.
I'm having trouble figuring out a way to make a function that is always running and can detect when that number variable changes.
Upvotes: 1
Views: 19135
Reputation: 116
If you are using functional components you can use the useEffect
hook,
like this
useEffect(()=>{
//call your increment function here
},[someVariable]) //and in the array tag the state you want to watch for
the useEffect
will cause a re-render when the state of 'someVariable' in the above example changes
you can see more about the hook in the react docs https://reactjs.org/docs/hooks-effect.html,
Upvotes: 7