State Management · Junior · Question 3 of 75

When should you use the functional update setCount(c => c + 1) instead of setCount(count + 1)?

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.

Further reading

Problem with a question

State Management · Junior · question 3

Test yourself

Take the State Management · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz