Below you can see some codesandbox example of react component , which just make simple count change(counter). I just try to log my new counter value , and I see , that it logs it twice on each button click, but why ? If I only change it once . Is there any explanation of this ?
function App() {
const [count, setCount] = useState(0);
const onClick = useCallback(() => {
setCount((prev) => prev + 1);
}, []);
console.log(count, "rerender");
return (
<div className="App">
<h1>{count}</h1>
<button onClick={onClick}>Click</button>
</div>
);
}
react: 18.0.0
react-dom: 18.0.0