Debug stale state with the snapshot model
Use React's state snapshot and update queue model to fix stale state bugs.
Stale update A '+3' button calls setCount(count + 1) three times. QA expects +3 and gets +1. The bug is not arithmetic. It is a mismatch with React's state snapshot model. State snapshots Render gives a snapshot; updater functions compose queued changes Handlers see the state from the render that created them. Updater functions tell React how to transform the latest queued value. Set value setCount(count + 1) three times The handler expresses the intended transformation instead of a stale target value. When next state depends on previous state, give React a function, not a stale value. 01 Symptom 02…
Sign up free — one personalized lesson every day, matched to your role and goals.
Already have an account? Sign in