For example, if I have components A and B, and component B is the child of component A:
<A>
<B />
</A>
Inside A we have:
useEffect(() => {
console.log('Parent A useEffect')
})
Inside B we have:
useEffect(() => {
console.log('Child B useEffect')
})
I did some tests and I saw 2 things:
- At the first load (after F5, for example), the log result is:
Parent A useEffect
Child B useEffect
- If we go to another component and then come back to component B (not by reloading but by using react-router, for example), the log result is:
Child B useEffect
Parent A useEffect
In two cases, the results are reversed. This makes me a bit confused.
I searched Google about componentDidMount
and I found this: https://github.com/facebook/react/blob/v15.0.1/docs/docs/ref-03-component-specs.md#mounting-componentdidmount
The
componentDidMount()
method of child components is invoked before that of parent components.
But I couldn't find the corresponding docs about useEffect
So what is the correct order of execution of useEffect
in parent/child components?