React---钩子函数

钩子函数的状态有4个阶段:         

                       <p>1.初始化阶段  (componentWillMount() || componentDidMount()) </p>
                      <p>2.更新阶段 (componentWillUpdate() || componentDidUpdate())</p>
                      <p>3.卸载阶段 (componentWillUnmount())</p>
                      <p>4.异常处理阶段 (componentWillCatch()) </p>

“钩子”就是在某个阶段给你一个做某些处理的机会。生命周期钩子函数就是在组件预备、创建、使用和销毁的过程中的函数监听。

React 组件生命周期:

componentWillMount :在渲染前调用,在客户端也在服务端。

componentDidMount:在第一次渲染后调用,只在客户端。之后组件已经生成了对应的DOM结构,可以通过this.getDOMNode()来进行访问。

componentWillReceiveProps:在组件接收到一个新的prop时被调用。这个方法在初始化render时不会被调用。

shouldComponentUpdate:返回一个布尔值。在组件接收到新的props或者state时被调用。在初始化时或者使用forceUpdate时不被调用,可以在你确认不需要更新组件时使用。

componentWillUpdate:在组件接收到新的props或者state但还没有render时被调用。在初始化时不会被调用。

componentDidUpdate:在组件完成更新后立即调用。在初始化时不会被调用。

componentWillUnmount:在组件从 DOM 中移除的时候立刻被调用。

猜你喜欢

转载自www.cnblogs.com/reeber/p/10821367.html