父子组件生命周期的执行顺序
时间: 2023-11-08 22:42:51 浏览: 151
在 React 中,父子组件的生命周期函数的执行顺序如下:
1. 父组件:
* constructor
* static getDerivedStateFromProps
* render
* componentDidMount
2. 子组件:
* constructor
* static getDerivedStateFromProps
* render
* componentDidMount
3. 父组件更新:
* static getDerivedStateFromProps
* shouldComponentUpdate
* render
* getSnapshotBeforeUpdate
* componentDidUpdate
4. 子组件更新:
* static getDerivedStateFromProps
* shouldComponentUpdate
* render
* getSnapshotBeforeUpdate
* componentDidUpdate
5. 父组件卸载:
* componentWillUnmount
6. 子组件卸载:
* componentWillUnmount
需要注意的是,子组件的生命周期函数的执行顺序是在父组件的生命周期函数执行完之后才会开始执行。同时,子组件的生命周期函数也会在父组件更新时被调用。
相关问题
父子组件生命周期执行顺序
在 React 中,父组件的生命周期函数总是先于子组件的生命周期函数执行,具体的执行顺序如下:
1. 父组件的 constructor
2. 父组件的 componentWillMount
3. 父组件的 render
4. 子组件的 constructor
5. 子组件的 componentWillMount
6. 子组件的 render
7. 子组件的 componentDidMount
8. 父组件的 componentDidMount
9. 父组件的 componentWillReceiveProps (如果有props更新)
10. 父组件的 shouldComponentUpdate (如果有props更新)
11. 父组件的 componentWillUpdate (如果有props更新)
12. 父组件的 render (如果有props更新)
13. 子组件的 componentWillReceiveProps (如果有props更新)
14. 子组件的 shouldComponentUpdate (如果有props更新)
15. 子组件的 componentWillUpdate (如果有props更新)
16. 子组件的 render (如果有props更新)
17. 子组件的 componentDidUpdate (如果有props更新)
18. 父组件的 componentDidUpdate (如果有props更新)
19. 父组件的 componentWillUnmount (如果组件卸载)
希望这能够帮助你理解父子组件的生命周期函数执行顺序。
vue 父子组件生命周期执行顺序
在Vue中,父子组件的生命周期钩子函数的执行顺序如下:
1. 父组件:
- beforeCreate:在实例初始化之后,数据观测 (data observer) 和 event/watcher 事件配置之前被调用。
- created:在实例创建完成后被立即调用。在这一步,实例已完成以下配置:数据观测 (data observer),属性和方法的运算,watch/event 事件回调。然而,挂载阶段还没开始,$el 属性目前不可见。
- beforeMount:在挂载开始之前被调用。相关的 render 函数首次被调用。
- mounted:el 被新创建的 vm.$el 替换,并挂载到实例上去之后调用该钩子。
- beforeUpdate:数据更新时调用,发生在虚拟 DOM 重新渲染和打补丁之前。可以在该钩子中对数据进行修改。
- updated:由于数据更改导致的虚拟 DOM 重新渲染和打补丁完成之后调用。
- beforeDestroy:实例销毁之前调用。在这一步,实例仍然完全可用。
- destroyed:Vue 实例销毁后调用。此时,Vue 实例的所有指令都已解绑,所有事件监听器都已移除。
2. 子组件:
- beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeDestroy、destroyed 这些生命周期钩子函数的执行顺序与父组件相同。
阅读全文
相关推荐
















