Instead of keeping a ref to the last derived state, bail in setDerivedState by returning the same last derived state.
This simplifies the code and has the same effect. Note that I had to change the test to use useEffect to track "renders", since according to https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-state-update the bailout will still run the function body, but won't call effects or render children, which are the expensive things.
Instead of keeping a ref to the last derived state, bail in setDerivedState by returning the same last derived state.
This simplifies the code and has the same effect. Note that I had to change the test to use
useEffect
to track "renders", since according to https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-state-update the bailout will still run the function body, but won't call effects or render children, which are the expensive things.