import "./styles.css";
import {useState} from 'react';
export default function App() {
const [count, setCount] = useState(0);
const [count2, setCount2] = useState(0);
console.log("Render: ", count, count2);
function update() {
setCount(count + 1);
setCount(count + 1);
}
function updateWithCB() {
setCount2(prevCount => prevCount + 1);
setCount2(prevCount => prevCount + 1);
}
return (
<div className="App">
<p>{count}</p>
<button onClick={update}>+</button>
<br />
<p>{count2}</p>
<button onClick={updateWithCB}>+ with CB</button>
</div>
);
}
I am a learning react and came across this behavior. When + is clicked it updates only once but when + with cb is clicked, it updates twice as intended. Does that mean useState function calls are merged and if so how to predict the merging when multiple useState calls are in use? And why doesn't it print 2 when + button is clicked once as it should? Any react specific reasons? Thanks