My problem is that I am doing a practice in react, I am using useState and a setInterval to be able to see how the state works. But when making the useState variable change so that it can be displayed on the screen, a count is displayed.
**import { useState } from "react";
function Estado() {
const [intervalo, setIntervalo] = useState(0);
setInterval(() => {
setInt(int + 1)
}, 400);
return <h1>{intervalo}</h1>;
}
export default Estado;**
In react you cannot leave the
setInterval
inside the component, because when it is executed, you are rendering the whole component again and even the setInterval, that is, resetting it.To use
setInterval
inside a component, Use it inside auseEffect
AND force the rerender with aclearInterval
, like this