React 组件生命周期
2017-10-26 09:55
579 查看
在本章节中我们将讨论 React 组件的生命周期。
组件的生命周期可分成三个状态:
Mounting:已插入真实 DOM
Updating:正在被重新渲染
Unmounting:已移出真实 DOM
生命周期的方法有:
componentWillMount 在渲染前调用,在客户端也在服务端。
componentDidMount : 在第一次渲染后调用,只在客户端。之后组件已经生成了对应的DOM结构,可以通过this.getDOMNode()来进行访问。 如果你想和其他JavaScript框架一起使用,可以在这个方法中调用setTimeout, setInterval或者发送AJAX请求等操作(防止异部操作阻塞UI)。
componentWillReceiveProps 在组件接收到一个新的prop时被调用。这个方法在初始化render时不会被调用。
shouldComponentUpdate 返回一个布尔值。在组件接收到新的props或者state时被调用。在初始化时或者使用forceUpdate时不被调用。
可以在你确认不需要更新组件时使用。
componentWillUpdate在组件接收到新的props或者state但还没有render时被调用。在初始化时不会被调用。
componentDidUpdate 在组件完成更新后立即调用。在初始化时不会被调用。
componentWillUnmount在组件从 DOM 中移除的时候立刻被调用。
这些方法的详细说明,可以参考官方文档。
以下实例在 Hello 组件加载以后,通过 componentDidMount 方法设置一个定时器,每隔100毫秒重新设置组件的透明度,并重新渲染:
varHello
=React.createClass({getInitialState:function(){return{opacity:1.0};},componentDidMount:function(){this.timer
= setInterval(function(){varopacity
=this.state.opacity;opacity
-=.05;if(opacity
< 0.1){opacity
=1.0;}this.setState({opacity:opacity});}.bind(this),100);},render:function(){return(
<divstyle={{opacity:this.state.opacity}}>Hello{this.props.name}
</div>);}});ReactDOM.render(
<Helloname="world"/>,document.body);
尝试一下
»
以下实例初始化 state , setNewnumber 用于更新 state。所有生命周期在 Content 组件中。
varButton
=React.createClass({getInitialState:function(){return{data:0};},setNewNumber:function(){this.setState({data:this.state.data
+ 1})},render:function(){return(
<div> <buttononClick
={this.setNewNumber}>INCREMENT</button>
<ContentmyNumber
={this.state.data}></Content>
</div>);}})varContent
=React.createClass({componentWillMount:function(){console.log('Component
WILL MOUNT!')},componentDidMount:function(){console.log('Component
DID MOUNT!')},componentWillReceiveProps:function(newProps){console.log('Component
WILL RECEIVE PROPS!')},shouldComponentUpdate:function(newProps,newState){returntrue;},componentWillUpdate:function(nextProps,nextState){console.log('Component
WILL UPDATE!');},componentDidUpdate:function(prevProps,prevState){console.log('Component
DID UPDATE!')},componentWillUnmount:function(){console.log('Component
WILL UNMOUNT!')},render:function(){return(
<div> <h3>{this.props.myNumber}</h3>
</div>);}});ReactDOM.render(
<div> <Button
/> </div>,document.getElementById('example'));
首次实例化
getDefaultProps
getInitialState
componentWillMount
render
componentDidMount
实例化完成后的更新
getInitialState
componentWillMount
render
componentDidMount
组件已存在时的状态改变
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
componentWillUnmount
生命周期共提供了10个不同的API。
作用于组件类,只调用一次,返回对象用于设置默认的
作用于组件的实例,在实例创建时调用一次,用于初始化每个实例的
在完成首次渲染之前调用,此时仍可以修改组件的state。
必选的方法,创建虚拟DOM,该方法具有特殊的规则:
只能通过
可以返回
只能出现一个顶级组件(不能返回数组)
不能改变组件的状态
不能修改DOM的输出
真实的DOM被渲染出来后调用,在该方法中可通过
在服务端中,该方法不会被调用。
组件接收到新的
组件是否应当渲染新的
在首次渲染期间或者调用了
接收到新的
完成渲染新的
组件被移除之前被调用,可以用于做一些清理工作,在
组件的生命周期可分成三个状态:
Mounting:已插入真实 DOM
Updating:正在被重新渲染
Unmounting:已移出真实 DOM
生命周期的方法有:
componentWillMount 在渲染前调用,在客户端也在服务端。
componentDidMount : 在第一次渲染后调用,只在客户端。之后组件已经生成了对应的DOM结构,可以通过this.getDOMNode()来进行访问。 如果你想和其他JavaScript框架一起使用,可以在这个方法中调用setTimeout, setInterval或者发送AJAX请求等操作(防止异部操作阻塞UI)。
componentWillReceiveProps 在组件接收到一个新的prop时被调用。这个方法在初始化render时不会被调用。
shouldComponentUpdate 返回一个布尔值。在组件接收到新的props或者state时被调用。在初始化时或者使用forceUpdate时不被调用。
可以在你确认不需要更新组件时使用。
componentWillUpdate在组件接收到新的props或者state但还没有render时被调用。在初始化时不会被调用。
componentDidUpdate 在组件完成更新后立即调用。在初始化时不会被调用。
componentWillUnmount在组件从 DOM 中移除的时候立刻被调用。
这些方法的详细说明,可以参考官方文档。
以下实例在 Hello 组件加载以后,通过 componentDidMount 方法设置一个定时器,每隔100毫秒重新设置组件的透明度,并重新渲染:
React 实例
varHello=React.createClass({getInitialState:function(){return{opacity:1.0};},componentDidMount:function(){this.timer
= setInterval(function(){varopacity
=this.state.opacity;opacity
-=.05;if(opacity
< 0.1){opacity
=1.0;}this.setState({opacity:opacity});}.bind(this),100);},render:function(){return(
<divstyle={{opacity:this.state.opacity}}>Hello{this.props.name}
</div>);}});ReactDOM.render(
<Helloname="world"/>,document.body);
尝试一下
»
以下实例初始化 state , setNewnumber 用于更新 state。所有生命周期在 Content 组件中。
React 实例
varButton=React.createClass({getInitialState:function(){return{data:0};},setNewNumber:function(){this.setState({data:this.state.data
+ 1})},render:function(){return(
<div> <buttononClick
={this.setNewNumber}>INCREMENT</button>
<ContentmyNumber
={this.state.data}></Content>
</div>);}})varContent
=React.createClass({componentWillMount:function(){console.log('Component
WILL MOUNT!')},componentDidMount:function(){console.log('Component
DID MOUNT!')},componentWillReceiveProps:function(newProps){console.log('Component
WILL RECEIVE PROPS!')},shouldComponentUpdate:function(newProps,newState){returntrue;},componentWillUpdate:function(nextProps,nextState){console.log('Component
WILL UPDATE!');},componentDidUpdate:function(prevProps,prevState){console.log('Component
DID UPDATE!')},componentWillUnmount:function(){console.log('Component
WILL UNMOUNT!')},render:function(){return(
<div> <h3>{this.props.myNumber}</h3>
</div>);}});ReactDOM.render(
<div> <Button
/> </div>,document.getElementById('example'));
实例化
首次实例化getDefaultProps
getInitialState
componentWillMount
render
componentDidMount
实例化完成后的更新
getInitialState
componentWillMount
render
componentDidMount
存在期
组件已存在时的状态改变componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
销毁&清理期
componentWillUnmount
说明
生命周期共提供了10个不同的API。
1.getDefaultProps
作用于组件类,只调用一次,返回对象用于设置默认的props,对于引用值,会在实例中共享。
2.getInitialState
作用于组件的实例,在实例创建时调用一次,用于初始化每个实例的state,此时可以访问
this.props。
3.componentWillMount
在完成首次渲染之前调用,此时仍可以修改组件的state。
4.render
必选的方法,创建虚拟DOM,该方法具有特殊的规则:只能通过
this.props和
this.state访问数据
可以返回
null、
false或任何React组件
只能出现一个顶级组件(不能返回数组)
不能改变组件的状态
不能修改DOM的输出
5.componentDidMount
真实的DOM被渲染出来后调用,在该方法中可通过this.getDOMNode()访问到真实的DOM元素。此时已可以使用其他类库来操作这个DOM。
在服务端中,该方法不会被调用。
6.componentWillReceiveProps
组件接收到新的props时调用,并将其作为参数
nextProps使用,此时可以更改组件
props及
state。
componentWillReceiveProps: function(nextProps) { if (nextProps.bool) { this.setState({ bool: true }); } }
7.shouldComponentUpdate
组件是否应当渲染新的props或
state,返回
false表示跳过后续的生命周期方法,通常不需要使用以避免出现bug。在出现应用的瓶颈时,可通过该方法进行适当的优化。
在首次渲染期间或者调用了
forceUpdate方法后,该方法不会被调用
8.componentWillUpdate
接收到新的props或者
state后,进行渲染之前调用,此时不允许更新
props或
state。
9.componentDidUpdate
完成渲染新的props或者
state后调用,此时可以访问到新的DOM元素。
10.componentWillUnmount
组件被移除之前被调用,可以用于做一些清理工作,在componentDidMount方法中添加的所有任务都需要在该方法中撤销,比如创建的定时器或添加的事件监听器。
相关文章推荐
- React:组件的生命周期
- react组件的生命周期
- React组件生命周期
- React组件生命周期
- React组件生命周期详解
- React:组件的生命周期
- react 组件的生命周期
- React组件生命周期
- React Native 中组件的生命周期
- React Native组件的生命周期
- React组件生命周期
- react组件的生命周期
- React Native组件生命周期
- [置顶] react组件生命周期详解
- react-native组件的生命周期
- react this.state + 组件生命周期
- React学习(二):组件的生命周期及数据流
- 【JAVASCRIPT】React学习-组件生命周期
- React组件的生命周期
- React组件的生命周期详解