DEV Community

Discussion on: Setting state for parent from within useEffect hook in child component causes an infinite loop

Collapse
 
roggc profile image
roggc

I think I found the reason why this happens. It is because we are using objects as state. When you set setState({...state,hey:true}) you are setting state to a new object, which has a new address, so state changes, so it re-renders, while if you set state as this for example setState(true), this would not cause an infinite loop because it will not cause a re-render of the App component when setting its state to true when it is already true.