It because when you use ({...currentData})
you literally create a new object, a new reference to a new object, and Javascript compare objects by reference, not value. So the new value of currentData
will always different from the previous once. That make the flow would look like this:
You setData (create new Object)
=> useEffect detect that currentData changed
=> it run into the call back, subscribe again
=> You setData (create new Object) again
=> useEffect detect that currentData changed
. And that cause the loop.
Currently, comparing deep nested object is not supported in React useEffect
. But you can overcome the issue using a custom hook, i found another answer that I think could help
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…