一、三个阶段

初始化阶段

  • constructor: 构造函数,初始化组件实例
  • getDefaultProps: 获取实例的默认属性(React.createClass())
  • getInitialState: 获取每个实例的初始化状态(React.createClass())
  • componentWillMount: 组件即将被挂载、渲染到页面上
  • render: 组件在这里生成虚拟的DOM节点
  • coponentDidMount: 组件真正在被挂载之后

运行中状态

  • componentWillReceiveProps: 组件将要接收到属性的时候调用
  • shouldComponentUpdate: 组件接收到新属性或者新状态的时候(可以返回false, 接收数据后不更新,阻止render调用,后面的函数不会继续执行了)
  • componentWillUpdate: 组件即将更新,不能修改属性和状态
  • render: 组件重新描绘
  • getSnapshotBeforeUpdate: 使得组件能在发生更改之前从 DOM 中捕获一些信息(例如,滚动位置)
  • componentDidUpdate: 组件已经更新,在这个生命周期里面不能使用setState( )

销毁阶段

  • componentWillUnmount:组件即将销毁

二、具体钩子详解

1.shouldComponentUpdate是做什么的?

shouldComponentUpdate 这个方法用来判断是否需要调用 render 方法重新描绘 DOM。因为 DOM的描绘非常消耗性能,如果我们能在 shouldComponentUpdate 方法中能够写出更优化的 dom diff 算法,可以极大的提高性能。


JacksonHuang
74 声望3 粉丝