引言

关于React的生命周期API,官网,有着详细说明。但在实际写代码的过程中,这些说明不能解决所有的疑惑。 所以我列举了一些编码中常见用例,供大家参考。

示例代码如下



/*
use case
1. mixin中重名生命周期方法
2. 重复React.render同个组件
3. 从 mount 到 unmount 再到 mount
4. 子组件两次加载
5. 父组件update
6. 改变子组件的包裹
*/ var IamMixinObject = {
componentDidMount: function(){
console.log('mixin里的componentDidMount');
}
}; var SonClass = React.createClass({
mixins: [IamMixinObject],
getDefaultProps:function(){
console.log("getDefaultProps");
},
componentWillReceiveProps :function(){
console.log("componentWillReceiveProps");
},
getInitialState:function(){
console.log("getInitialState");
return {};
},
componentWillMount:function(){
console.log("componentWillMount");
},
componentWillUpdate:function(){
console.log("componentWillUpdate");
},
shouldComponentUpdate:function(){
console.log("shouldComponentUpdate");
return true;
},
render: function(){
console.log("render");
return null;
},
componentDidUpdate: function(){
console.log('componentDidUpdate');
},
componentDidMount: function(){
console.log('componentDidMount');
},
componentWillUnmount: function(){
console.log('componentWillUnmount');
}
}); // 1. mixin中重名生命周期方法
React.render(<SonClass />, document.getElementById("forTest"));
/*
mixin里的componentDidMount 会先于 componentDidMount输出。
其实,mixin中和组件同名的生命周期方法,都是mixin中先执行。
*/ // 2. 重复React.render同个组件
React.render(<SonClass />, document.getElementById("forTest"));
React.render(<SonClass />, document.getElementById("forTest"));
/*
输出:
getDefaultProps
getInitialState
componentWillMount
render
mixin里的componentDidMount
componentDidMount componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
总结:
第二次mount同个render组件,相当于改变 组件props。
*/ // 3. 从 mount 到 unmount 再到 mount
React.render(<SonClass />, document.getElementById("forTest"));
React.unmountComponentAtNode(document.getElementById("forTest"));
React.render(<SonClass />, document.getElementById("forTest"));
/*
输出:
getDefaultProps
getInitialState
componentWillMount
render
mixin里的componentDidMount
componentDidMount
componentWillUnmount getInitialState
componentWillMount
render
mixin里的componentDidMount
componentDidMount
总结:
unmount的时候,确实调用了componentWillUnmount方法,第二次mount的时候,并没有执行getDefaultProps方法。
是因为,getDefaultProps Invoked once and cached when the class is created。
它在组件类创建的时候被调用一次,其返回值会被缓存。
*/ // 4. 子组件两次装载
var FatherClass = React.createClass({
render:function(){ return (
<div>
<SonClass />
<SonClass />
</div>
)
}
});
React.render(<FatherClass />, document.getElementById("forTest"));
/*
输出:
getDefaultProps
getInitialState
componentWillMount
render
getInitialState
componentWillMount
render mixin里的componentDidMount
componentDidMount
mixin里的componentDidMount
componentDidMount
总结:
发现两次componentDidMount是后面连续触发。
这里涉及到React的batchUpdate机制,需要另一篇文章详解。
大概机制是,
因为setState是异步的。
它把变化存入一个临时队列,渲染完新的内容后才调用所有 componentDidUpdate或componentDidMount。 */ // 5. 父组件update
var FatherClass = React.createClass({
getInitialState:function(){ return {
changeFlag:false
};
},
changeSon:function(){
var changeFlag = this.state.changeFlag;
this.setState({
changeFlag: !changeFlag
});
},
render:function(){
return (
<div onClick={this.changeSon} >
<SonClass />
</div>
)
}
});
React.render(<FatherClass />, document.getElementById("forTest"));
/*
输出:
getDefaultProps
getInitialState
componentWillMount
render
mixin里的componentDidMount
componentDidMount componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
总结:
父组件setState了,对于子组件是setProps了
*/ // 6. 改变子组件的包裹
var FatherClass = React.createClass({
getInitialState:function(){ return {
changeFlag:false
};
},
changeSon:function(){
var changeFlag = this.state.changeFlag;
this.setState({
changeFlag: !changeFlag
});
},
render:function(){
var changeFlag = this.state.changeFlag;
var Son = changeFlag ? <SonClass /> : <div>new son<SonClass /></div> return (
<div onClick={this.changeSon} >
父组件在此
{
Son
}
</div>
)
}
});
React.render(<FatherClass />, document.getElementById("forTest"));
/*
输出:
getDefaultProps
getInitialState
componentWillMount
render
mixin里的componentDidMount
componentDidMount componentWillUnmount getInitialState
componentWillMount
render
mixin里的componentDidMount
componentDidMount
总结:
父组件setState改变了子组件的包裹,子组件相当于重新mount */

React生命周期浅析的更多相关文章

  1. React生命周期

    在react生命周期中,分2段执行,一个挂载的生命周期,一个是组件发生了数据变动,或者事件触发而引发的更新生命周期. 注:react生命周期很重要,对于很多组件场景的应用发挥重要作用,而且不熟悉生命周 ...

  2. React 生命周期

    前言 学习React,生命周期很重要,我们了解完生命周期的各个组件,对写高性能组件会有很大的帮助. Ract生命周期 React 生命周期分为三种状态 1. 初始化 2.更新 3.销毁 初始化 1.g ...

  3. React生命周期详解

    React生命周期图解: 一.旧版图解: 二.新版图解: 从图中,我们可以清楚知道React的生命周期分为三个部分:  实例化.存在期和销毁时. 旧版生命周期如果要开启async rendering, ...

  4. React生命周期简单详细理解

    前言 学习React,生命周期很重要,我们了解完生命周期的各个组件,对写高性能组件会有很大的帮助. Ract生命周期 React 生命周期分为三种状态 1. 初始化 2.更新 3.销毁 初始化 1.g ...

  5. 22.1 、react生命周期(一)

    在每个react组件中都有以下几个生命周期方法~我们需要在不同阶段进行讨论 组件生命周期概述 1.初始化 在组件初始化阶段会执行 constructor static getDerivedStateF ...

  6. react 生命周期钩子里不要写逻辑,否则不生效

    react 生命周期钩子里不要写逻辑,否则不生效,要把逻辑写在函数里,然后在钩子里调用函数,否则会出现问题.

  7. react复习总结(2)--react生命周期和组件通信

    这是react项目复习总结第二讲, 第一讲:https://www.cnblogs.com/wuhairui/p/10367620.html 首先我们来学习下react的生命周期(钩子)函数. 什么是 ...

  8. React生命周期执行顺序详解

    文章内容转载于https://www.cnblogs.com/faith3/p/9216165.html 一.组件生命周期的执行次数是什么样子的??? 只执行一次: constructor.compo ...

  9. vue生命周期和react生命周期对比

    一 vue的生命周期如下图所示(很清晰)初始化.编译.更新.销毁 二 vue生命周期的栗子 注意触发vue的created事件以后,this便指向vue实例,这点很重要 <!DOCTYPE ht ...

随机推荐

  1. 关于学习keynote

    下午在学习如何用keynote写出高大上的文档,看到公司内的一个妹纸洋洋洒洒的写了好多篇文章,顿时觉得自己的知识面狭窄,文科女和理科女的差别,从我嘴里半天吐不出一个富有诗情画意的词句来,那么还是脚踏实 ...

  2. 160922、配置:spring通过profile或@profile配置不同的环境(测试、开发、生产)

    一.配置环境 applicationContext.xml中添加下边的内容(develop:开发环境,production:生产环境,test:测试环境) 注意:profile的定义一定要在文档的最下 ...

  3. 我的Windows naked apps

    0. 驱动精灵全能网卡版 1. Microsoft Office 2010/2013 2. IE 11 3. Filezilla Client & Server 4. Google Chrom ...

  4. jython学习笔记2

    1.%是求余,//是整除的商,**是乘方 abs(var) Absolute value pow(x, y) Can be used in place of ** operator pow(x,y,m ...

  5. Dijkstra 算法、Kruskal 算法、Prim算法、floyd算法

    1.dijkstra算法 算最短路径的,算法解决的是有向图中单个源点到其他顶点的最短路径问题. 初始化n*n的数组. 2.kruskal算法 算最小生成树的,按权值加入 3.Prim算法 类似dijk ...

  6. JavaEE基础(六)

    1.面向对象(面向对象思想概述) A:面向过程思想概述 第一步 第二步 B:面向对象思想概述 找对象(第一步,第二步) C:举例 买煎饼果子 洗衣服 D:面向对象思想特点 a:是一种更符合我们思想习惯 ...

  7. 使用Xcode和Instruments调试解决iOS内存泄露

    转载自:http://www.uml.org.cn/mobiledev/201212123.asp  (或者http://www.cocoachina.com/bbs/read.php?tid=129 ...

  8. Jquery知识

    1.窗口自适应调整 (设置layout的fit属性值为true即可) //窗口自适应调整 $(function() { windowResize(); //文档载入时加载 $(window).resi ...

  9. android基础小结

    (注:此小结文档在全屏模式下观看效果最佳) 2016年3月1日,正式开始了我的android学习之路. 最最开始的,当然是学习怎样搭载环境了,然而苦逼的我在win10各种坑爹的指引下还是安装了一个星期 ...

  10. Python time clock()方法

    描述 Python time clock() 函数以浮点数计算的秒数返回当前的CPU时间.用来衡量不同程序的耗时,比time.time()更有用. 这个需要注意,在不同的系统上含义不同.在UNIX系统 ...