State Management · Junior · Question 3 of 75
When should you use the functional update setCount(c => c + 1) instead of setCount(count + 1)?
- ✓Correct answer: When the new value depends on the previous one, especially with several updates in a row
- 2Only in class components; function components don't support it
- 3When the state holds a string rather than a number
Explanation
count is a snapshot of the current render. Three setCount(count + 1) calls in one handler produce +1, while three setCount(c => c + 1) produce +3, because each updater receives the result of the previous one. It also protects against stale closures in timers and effects.