Does componentDidUpdate run after all children have been updated?

前端 未结 3 1246
一整个雨季
一整个雨季 2020-12-31 04:18

I would like to know if a React component\'s lifecycle method componentDidUpdate gets executed after all of the children\'s render methods have fin

3条回答
  •  太阳男子
    2020-12-31 04:29

    The componentDidUpdate method is called after the render method of the component is done executing. That means that it will be called after all children's render methods have finished. This is implied in the documentation you linked:

    Use this as an opportunity to operate on the DOM when the component has been updated.

    The component is only updated post-render, so the documentation implies that it's called after all children, and consequently the parent, have finished rerendering (albeit a bit unclear). You can only really operate on the DOM when it finishes updating, children and all.

    For example, say we have two components, A and B and B renders a A component. componentDidUpdate for B will only be called once B's render finishes. The render of B will finish after A's render is successfully called because children are rendered first due to being part of the parent. That means the answer to your question is: componentDidUpdate is executed after all the children's renders have completed.

提交回复
热议问题