setState()
from useState
is executed asynchronously. Once the state is actually set it will trigger a re-render. Your second console.log
will not reliably have the accurate state
.
If you want something else to happen after state
has been set you can use a useEffect
like this.
useEffect(() => {
console.log("State is:", state);
},[state]);
This will fire on load, and every time state is updated.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…