State update quick reference
Recall practical rules for React state snapshots, previous-state updates, and immutable object or array changes.
Snapshot What does 'state is a snapshot' mean in an event handler? The handler sees the state value from the render that created it. Multiple value-form updates in one handler may read the same old value. Previous state Which update should you use when next state depends on previous state? Use updater functions for queued previous-state transformations. Mutation objection I changed the object and then called the setter, so React should update. The object in state was mutated in place. Your response Create the next object and pass that to the setter: setUser({ ...user, name }). Calling the setter with…
Sign up free — one personalized lesson every day, matched to your role and goals.
Already have an account? Sign in