React 生命周期
-
挂载阶段(Mounting) :
constructor()static getDerivedStateFromProps()render()componentDidMount()
-
更新阶段(Updating) :
static getDerivedStateFromProps()shouldComponentUpdate()render()getSnapshotBeforeUpdate()componentDidUpdate()
-
卸载阶段(Unmounting) :
componentWillUnmount()
Vue 生命周期
-
创建阶段:
beforeCreate()created()
-
挂载阶段:
beforeMount()mounted()
-
更新阶段:
beforeUpdate()updated()
-
销毁阶段:
beforeDestroy()destroyed()
页面数据获取最佳生命周期
- React:
componentDidMount是获取数据的最佳时机,因为此时组件已经挂载,可以安全地进行 DOM 操作和异步请求。 - Vue:
mounted是获取数据的最佳时机,因为此时组件已经被挂载,可以安全地进行 DOM 操作和异步请求。