Can you please explain me why this simple code doesn't work as expected ? There's a button called "Start the timer" that's directing to the "chrono" function. I thought that using setCount would update the count variable state but it stays at 0 every second. Isn't useState used precisely to update variables ? Thanks for your help ! PS : in the button, I just rename onClick to clicAction to use it in my component but this is not a problem at all.
import { Button } from './components/Button';
import { useState, useEffect } from 'react';
function App() {
const [count, setcount] = useState(0);
const chrono = () => {
setInterval(() => {
setcount(count +1);
console.log(count);
},
1000);
}
return (
<>
<Button clicAction={chrono} nom="Start the timer"/>
<p>{count}</p>
</>
);
}
export default App;
And here's the Button component code :
import { useState } from 'react';
export const Button = ({nom, clicAction}) => {
return (
<div>
<button onClick={clicAction}>{nom}</button>
</div>
)
}