DEV Community

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

Collapse
 
opshack profile image
Pooria A

Seems quite normal and expected to me. Changing the state in the child will trigger another render for the child because state is sent as a prop to it and it will repeat again.

useEffect in the way you used it is equivalent to componentDidMount

Collapse
 
roggc profile image
roggc • Edited

Either if you don't pass state as a prop you still get an infinite loop. I think when parent component re-renders, child component unmount and mounts, so is, as you say, componentDidMount, so that's why useEffect code always executes.