I have them hosted here on this page under the sections titled 'BAD: Don't set state that relies on the previous state without using the function of previous state' and 'GOOD: When setting state that relies on the previous state, do so as a function of previous state'.
You can verify this by trying out the two code snippets from my article. In both cases, the first set of implementations is risky and can lead to bugs, while the second set of implementations will be bug free even if state updates are batched. That's the exact same logic behind incrementing a counter which you provided as a counterexample.ĭoing toggleValue(!value) rather than toggleValue(value => !value) is the equivalent of doing incrementCounter(value + 1) rather than incrementCounter(value => value + 1). Not quite! When reversing a boolean value, we absolutely do care what the previous value was.