2 ms·
Posting code on the internet - dangerous business! I think the counter updates should use functional updates, since they depend on the current state. My unders
by jrh206 6y ago
Posting code on the internet - dangerous business!
I think the counter updates should use functional updates, since they depend on the current state. My understanding is that this can lead to a potential race condition, although I'm not 100% on the details.
setCounter(currentCounter => currentCounter + 1)
https://reactjs.org/docs/hooks-reference.html#functional-updates https://reactjs.org/docs/hooks-reference.html#functional-upd...
Interestingly your code is similar to some the first code in the basic 'into to hooks' section of the docs. Perhaps it's not a huge issue? Maybe somebody else who knows a bit more about React internals can chime in.
- chvid 6y agoThe code I wrote is perfectly fine. There is no possibility of race conditions in the code I wrote.