首先, 当面对一些问题:html
1 React 用了这么久,常常遇到的问题是setState
在这里写合适吗?react
2 为何setState
写在这里形成了重复渲染屡次?git
3 为何你的setState
用的这么乱?github
4 组件传入props
是更新呢?从新挂载呢?仍是怎样?面试
5 ...redux
其次, 生命周期能够看到整个Component的运行过程, 在coding的时候很天然的找好他们的位置, 合做起来就会方便多了,这里极力推荐airbnb的react coding 规范.服务器
因此整理了这篇文章。若是错误,欢迎指正。网络
componentWillMount()
执行场景session
在render()
方法以前异步
解释
1 由于componentWillMount是在render以前执行,因此在这个方法中setState
不会发生从新渲染(re-render);
2 这是服务端渲染(server render
)中惟一调用的钩子(hook);
3 一般状况下,推荐用constructor()
方法代替;
render()
执行场景
1 在componentWillMount()
方法以后
2 在componentWillReceive(nextProps, nextState)
方法以后
解释
==
componentDidMount()
执行场景
在render()
方法以后
解释
1 这个方法会在render()以后当即执行;
2 这里能够对DOM进行操做,这个函数以后ref变成实际的DOM(@TODO 表述可能不清晰);
3 这里能够加载服务器数据,而且若是使用了redux之类的数据服务,这里能够出发加载服务器数据的action;
4 这里可使用setState()
方法触发从新渲染(re-render)
;
componentWillReceiveProps(nextProps)
执行场景
在已经挂在的组件(mounted component)接收到新props时触发;
简单的说是在除了第一次生命周期(componentWillMount -> render -> componentDidMount)以后的生命周期中出发;
解释
1 若是你须要在props
发生变化(或者说新传入的props)来更新state
,你可能须要比较this.props
和nextProps
, 而后使用this.setState()
方法来改变this.state
;
注意
1 React可能会在porps传入时即便没有发生改变的时候也发生从新渲染, 因此若是你想本身处理改变,请确保比较props当前值和下一次值; 这可能形成组件从新渲染;
2 若是你只是调用this.setState()
而不是从外部传入props
, 那么不会触发componentWillReceiveProps(nextProps)
函数;这就意味着: this.setState()
方法不会触发componentWillReceiveProps()
, props
的改变或者props
没有改变才会触发这个方法;
shouldComponentUpdate(nextProps, nextState)
执行场景
在接收到新props
或state
时,或者说在componentWillReceiveProps(nextProps)
后触发
解释
在接收新的props
或state
时肯定是否发生从新渲染,默认状况返回true
,表示会发生从新渲染
注意
1 这个方法在首次渲染时或者forceUpdate()
时不会触发;
2 这个方法若是返回false
, 那么props
或state
发生改变的时候会阻止子组件发生从新渲染;
3 目前,若是shouldComponentUpdate(nextProps, nextState)
返回false
, 那么componentWillUpdate(nextProps, nextState)
, render()
, componentDidUpdate()
都不会被触发;
4 Take care
: 在将来,React可能把shouldComponentUpdate()
当作一个小提示(hint)而不是一个指令(strict directive),而且它返回false
仍然可能触发组件从新渲染(re-render);
Good Idea
在React 15.3之后, React.PureComponent
已经支持使用,我的推荐,它代替了(或者说合并了)pure-render-mixin
,实现了shallowCompare()
。扩展阅读
componentWillUpdate(nextProps, nextState)
执行场景
在props
或state
发生改变或者shouldComponentUpdate(nextProps, nextState)
触发后, 在render()
以前
解释
1 这个方法在组件初始化时不会被调用;
注意
1 千万不要在这个函数中调用this.setState()
方法.;
2 若是确实须要响应props
的改变,那么你能够在componentWillReceiveProps(nextProps)
中作响应操做;
3 若是shouldComponentUpdate(nextProps, nextState)
返回false
,那么componentWillUpdate()
不会被触发;
componentDidUpdate(prevProps, prevState)
执行场景
在发生更新或componentWillUpdate(nextProps, nextState)
后
解释
1 该方法不会再组件初始化时触发;
2 使用这个方法能够对组件中的DOM进行操做;
3 只要你比较了this.props
和nextProps
,你想要发出网络请求(nextwork requests)时就能够发出, 固然你也能够不发出网络请求;
注意
若是shouldComponentUpdate(nextProps, nextState)
返回false
, 那么componentDidUpdate(prevProps, prevState)
不会被触发;
componentWillUnmount()
执行场景
在组件卸载(unmounted)或销毁(destroyed)以前
解释
这个方法可让你处理一些必要的清理操做,好比无效的timers、interval,或者取消网络请求,或者清理任何在componentDidMount()
中建立的DOM元素(elements);
setState(Object/Function)
解释
经过事件(event handlers)或服务请求回调(server request callbacks), 触发UI更新(re-render);
参数
1 能够是Object
类型, 这时是异步的setState, 同时接收一个在state发生改变以后的回调, 如this.setState(Object, callback)
, 其中callback能够是() => { ... this.state ... }
;
2 能够是Function
类型, 这时是同步的setState, 例如: (prevState, prevProps) => nextState
, 同步存在必定效率问题(不理解), 可是它有一个好处就是支持Immutable
;
缘由
组件第一次创建
触发
componentWillMount -> render -> componentDidMount
缘由
props
发生改变
触发
componentWillReceiveProps -> shouldComponentUpdate -> componentWillUpdate -> componentDidUpdate
缘由
this.setState()
使state
发生改变
触发
shoudlComponentUpdate -> componentWillUpdate -> componentDidUpdate
缘由
组件卸载或销毁
触发
componentWillUnmount