react 生命周期函数是什么
react 生命周期函数
初始化
1.getDefaultProps()
注:getDefaultProps这种定义方式是用在你定义组件用的是React.createClass方式的
假如使用的是es6的语法,例如用的是class 组件名 extends React.Component的话,就不要用
getDefaultProps这种方式去定义props了,而是应当用 static propTypes ={}来定义,这样就不会有警了
设定默许的props,也可以用dufaultProps设定组件的默许属性. ---》设定
2.getInitialState()
注:与getDefaultProps的不同在于前者设定默许的 props,后者设定初始的state在使用es6的class语法时是没有这个钩子函数的,可以直接在constructor中定义 this.state。此时可以拜访this.props
3.componentWillMount
注:组件初始化时只调取,今后组件更新不调取,整个生命周期只调取一次,此时可以修改state。
4. render()
注:react最重要的步骤,创立虚拟dom,停止diff算法,更新dom树都在此停止。此时就不克不及更换state了。
5.componentDidMount()
注:组件渲染之后调取,只调取一次。可以在此恳求数据
·更新
1.componentWillReceiveProps(nextProps)
注:组件初始化时不调取,组件接受新的props时调取。
2.shouldComponentUpdate(nextProps, nextState)
注:react机能优化非常重要的一环。组件接受新的state或者props时调取,我们可以设定在此对照前后两个props和state可否雷同,假如雷同则返回false阻挠更新,由于雷同的属性状态必然会生成雷同的dom树,这样就不需要制造新的dom树和旧的dom树停止diff算法对照,节约大量机能,特别是在dom构造复杂的时候
3.componentWillUpdata(nextProps, nextState)
注:组件初始化时不调取,只要在组件将要更新时才调取,此时可以修改state
4.render()
注:组件渲染
5.componentDidUpdate()
注:组件初始化时不调取,组件更新完成后调取,此时可以猎取dom节点。
·卸载
componentWillUnmount()
注:组件将要卸载时调取,一些事件监听和按时器需要在此时清除。
引荐教程:《react教程》
以上就是react 生命周期函数是什么的具体内容,更多请关注百分百源码网其它相关文章!