您的位置:首页 > Web前端 > React

React生命周期

2017-02-06 17:20 337 查看
组件会随着组件的props和state改变而发生变化,它的DOM也会有相应的变化。

一个组件就是一个状态机:对于特定的输入,它总会返回一致的输出。

React组件提供了生命周期的钩子函数去响应组件不同时刻的状态,组件的生命周期如下:

实例化

存在期

销毁期

实例化

首次调用组件时,有以下方法会被调用(注意顺序,从上到下先后执行):

getDefaultProps

这个方法是用来设置组件默认的props,组件生命周期只会调用一次。但是只适合React.createClass直接创建的组件,使用ES6/ES7创建的这个方法不可使用,ES6/ES7可以使用下面方式:

//es7
class Component {
static defaultProps = {}
}
//或者也可以在外面定义es6
//Compnent.defaultProps


getInitialState

设置state初始值,在这个方法中你已经可以访问到this.props。getDefaultProps只适合React.createClass使用。使用ES6初始化state方法如下:

class Component extends React.Component{
constructor(){
this.state = {
render: true,
}
}
}


componentWillMount

改方法会在组件首次渲染之前调用,这个是在render方法调用前可修改state的最后一次机会。这个方法很少用到。

render

这个方法以后大家都应该会很熟悉,JSX通过这里,解析成对应的虚拟DOM,渲染成最终效果。格式大致如下:

class Component extends React.Component{
render(){
return (
<div></div>
)
}
}




componentDidMount

这个方法在首次真实的DOM渲染后调用(仅此一次)当我们需要访问真实的DOM时,这个方法就经常用到。如何访问真实的DOM这里就不想说了。当我们需要请求外部接口数据,一般都在这里处理。

存在期

实例化后,当props或者state发生变化时,下面方法依次被调用:

componentWillReceiveProps

没当我们通过父组件更新子组件props时(这个也是唯一途径),这个方法就会被调用。

componentWillReceiveProps(nextProps){}


shouldComponentUpdate

字面意思,是否应该更新组件,默认返回true。当返回false时,后期函数就不会调用,组件不会在次渲染。

shouldComponentUpdate(nextProps,nextState){}


componentWillUpdate

字面意思组件将会更新,props和state改变后必调用。

render

跟实例化时的render一样,不多说

componentDidUpdate

这个方法在更新真实的DOM成功后调用,当我们需要访问真实的DOM时,这个方法就也经常用到。

销毁期

销毁阶段,只有一个函数被调用:

componentWillUnmount

没当组件使用完成,这个组件就必须从DOM中销毁,此时该方法就会被调用。当我们在组件中使用了setInterval,那我们就需要在这个方法中调用clearTimeout。

下面是一段测试代码

/*
* 最上层Component
*/
class Components extends React.Component {

constructor(props){
super(props);
this.state = {}
}
componentWillMount(){
console.debug("实例化:componentWillMount")
}
componentDidMount(){
console.debug("实例化:componentDidMount")
}
componentWillReceiveProps(){
console.debug("存在期:componentWillReceiveProps")
}
shouldComponentUpdate(nextProps,nextState){
console.debug("存在期:shouldComponentUpdate",nextProps,nextState)
return true;
}
componentWillUpdate(){
console.debug("存在期:componentWillUpdate")
}
componentDidUpdate(){
console.debug("存在期:componentDidUpdate")
}
render() {
if(!this.props.reRender){
console.debug("实例化:render")
}else{
console.debug("存在期:render")
}
return (
<div>
<br />
请查看下面的console
<br />
</div>
)

}
}
Components.defaultProps = {
text: "hello word",
}
class App extends React.Component{
constructor(props){
super(props);
this.state = {}
}
refresh(){
return (e)=>{
this.setState({
reRender: true,
})
}
}
render(){
return (
<div>
<Components reRender={this.state.reRender}/>
<button onClick={this.refresh()}>
更新Component
</button>
</div>
)
}
}
//这个组件是没有调用的,但是,getDeafultProps直接执行了
var Test = React.createClass({
getDefaultProps: function(){
console.debug("Test组件为调用,直接执行了defaultProps")
return {

}
},
render(){
return (
<div></div>
)
},
})
/*
* Render the above component into the div#app
*/
ReactDOM.render(<App />, document.getElementById('app'));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: