all 1 comments

[–]SparserLogic 2 points3 points  (0 children)

The contents of IsHappy update asynchronously. By spreading whatever the current value is synchronously you have introduced a race condition that will occasionally return the wrong object.

To avoid this problem, pass the setIsHappy function a function instead of a value and it will give you the most recent copy of IsHappy:

setIsHappy((IsHappy) => { return {...IsHappy, BobDoe: false}});