React:componentWillUpdate

Ovi*_*ota 4 reactjs

有人可以解释下一个背景下真正发生的事情:

假设我有一个App组件(父级),有些子组件让我们说索引,搜索,关于这三个孩子都有自己的孩子.

当我在App Component中调用componentWillUpdate时会发生什么...,新的状态/ props将会出现但是叶子(子)内会发生什么,他们会自己重新渲染吗?

并且假设我在App中的这个componentWillUpdate中覆盖了我的DOM,而且我在componentDidMount或componentWillMount中的叶子组件中的DOM也是一样的......哪个值将是最后一个?

Rad*_*těj 5

首先,你不应该componentWillUpdate手动调用.它被React调用,如果它是生命周期的一部分.

根据ReactCompositeComponent.js中的评论,这是更新阶段的问题:

Update Phases:
        - componentWillReceiveProps (only called if parent updated)
        - shouldComponentUpdate
          - componentWillUpdate
            - render
            - [children's constructors or receive props phases]
          - componentDidUpdate
Run Code Online (Sandbox Code Playgroud)

正如你所看到的App.render,在渲染儿童copmonents之前被调用.

如果由于某种原因想要手动操作DOM componentWillUpdate,那么这不是正确的做法.使用componentDidUpdate每次更新之后或componentDidMount初始后呈现.儿童componentDidUpdatecomponentDidMount在父母的生命周期方法之前触发.