Capítulo 24 de 116
React batches state updates within a single event handler (one re-render for all of them, not one per call), and when you need several updates to the same variable to actually compound before the next render, pass an updater function (setN(n => n + 1)) instead of a plain next-value.
set calls inside one event handler don't each trigger a separate render — React waits until the handler finishes, then re-renders once with all the updates applied. This keeps the UI from doing wasted intermediate renders and avoids a component rendering with only "half" its handler's updates applied.setNumber(number + 1) called three times in a row all read the same fixed number from this render's snapshot (Ch 23) — so they all request the same next value, not three increments.setNumber(n => n + 1) tells React "queue this transformation" instead of "replace with this value." React processes queued updater functions in order during the next render, feeding each one's return value as the n argument to the next — so three queued n => n + 1 calls genuinely produce +3.n for number, prevX also common) to keep handlers concise and readable.<button onClick={() => {
setNumber(n => n + 1);
setNumber(n => n + 1);
setNumber(n => n + 1);
}}>+3</button>
setX(x => ...)) whenever a state update depends on the state's previous value — it's the only form immune to the same-render "stale value" trap.set calls in one handler = one re-render," which is why relying on an intermediate re-render happening mid-handler is a bug, not a valid strategy.useState mechanics this chapter builds on.