React·Intermediate·Coding·1 min read
What is a functional state update?
Short interview answer
Passing an updater such as setCount(c => c + 1) asks React to calculate from the queued previous state. It avoids stale snapshots when multiple updates are batched or callbacks intentionally do not depend on a particular render's value.
Example
setCount(count + 1);setCount(count + 1); // both read the same snapshot -> +1 total
setCount((c) => c + 1);setCount((c) => c + 1); // each builds on the queued value -> +2 totalKey takeaway
Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.