I have a simple component with useState
that increase a counter in each click -
function Counter() {
let [counter, setCounter] = useState(0);
const incCounter = () => {
setCounter(counter + 1);
};
return (
<div className="App">
<h1>{counter}</h1>
<button onClick={incCounter}>Inc</button>
</div>
);
}
and now I want to call the increase function each 1 second , so I added this piece of code into the component function -
useEffect(() => {
setInterval(() => {
incCounter();
}, 1000);
}, []);
but I don't see the counter increased in the component.
How should I write it correctly and see the counter increased in each 1 second as expected ?