上流程图描述了组件从建立、运行到销毁的整个过程,能够看到若是一个组件在被建立,从开始一直到运行会依次调用getDefaultProps
到render
这五个函数;在运行过程当中,若是有属性和状态的改变,又会触发左侧的其余函数的调用,并在此回到运行状态;当组件即将会被销毁时,会调用函数conponentWillUnmount
来通知组件,到最终组件销毁,生命周期结束。安全
getDefaultProps 获取默认属性,并初始化props;
getInitialState 获取初始化的组件状态state;
componentWillMount 组件将会被装载,在渲染render前调用;
componentWillReceiveProps 若是接收到属性就会调用该方法,旧的属性仍然能够经过this.props来获取,也能够调用this.setState来更新组件的状态,这里更新状态是安全的,不会触发render。
shouldComponentUpdate 决定是否更新组件;
componentWillUpdate 若是组件的状态或者属性改变了,而且shouldComponentUpdate为true,就会调用侧方法准备更新组件;
render渲染,即初次渲染和更新组件的方法;
componentDidUpdate 组件更新完成后会调用此方法;
conponentWillUnmount 当组件要销毁,即从界面移除时,就会调用此方法。
在ES6中已经废除了getDefaultProps
和getInitialState
的方式,直接经过this.props
和this.state
来获取。函数