0

What is the best practice to set a new state based on the current state?

E.g.:

setCount(count + 1)

is it correct? What the risks?

Ralf O
  • 5
  • 2
  • This question is a bit different than the one referenced for closing. The one referenced is in relation to class components whereas this one is in relation to functional components and the hooks API. – Ross Sheppard Mar 03 '21 at 01:02
  • @RossSheppard See the second linked question, which references hooks. The general issue is the same, though. Use the callback form when the state the running function currently has a reference to may be stale. – CertainPerformance Mar 03 '21 at 01:07
  • Yes, the newly linked answer makes more sense now and, of course, I would say the same. Thanks – Ross Sheppard Mar 03 '21 at 01:58

0 Answers0