React生命周期浅析
引言
关于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生命周期浅析的更多相关文章
- React生命周期
在react生命周期中,分2段执行,一个挂载的生命周期,一个是组件发生了数据变动,或者事件触发而引发的更新生命周期. 注:react生命周期很重要,对于很多组件场景的应用发挥重要作用,而且不熟悉生命周 ...
- React 生命周期
前言 学习React,生命周期很重要,我们了解完生命周期的各个组件,对写高性能组件会有很大的帮助. Ract生命周期 React 生命周期分为三种状态 1. 初始化 2.更新 3.销毁 初始化 1.g ...
- React生命周期详解
React生命周期图解: 一.旧版图解: 二.新版图解: 从图中,我们可以清楚知道React的生命周期分为三个部分: 实例化.存在期和销毁时. 旧版生命周期如果要开启async rendering, ...
- React生命周期简单详细理解
前言 学习React,生命周期很重要,我们了解完生命周期的各个组件,对写高性能组件会有很大的帮助. Ract生命周期 React 生命周期分为三种状态 1. 初始化 2.更新 3.销毁 初始化 1.g ...
- 22.1 、react生命周期(一)
在每个react组件中都有以下几个生命周期方法~我们需要在不同阶段进行讨论 组件生命周期概述 1.初始化 在组件初始化阶段会执行 constructor static getDerivedStateF ...
- react 生命周期钩子里不要写逻辑,否则不生效
react 生命周期钩子里不要写逻辑,否则不生效,要把逻辑写在函数里,然后在钩子里调用函数,否则会出现问题.
- react复习总结(2)--react生命周期和组件通信
这是react项目复习总结第二讲, 第一讲:https://www.cnblogs.com/wuhairui/p/10367620.html 首先我们来学习下react的生命周期(钩子)函数. 什么是 ...
- React生命周期执行顺序详解
文章内容转载于https://www.cnblogs.com/faith3/p/9216165.html 一.组件生命周期的执行次数是什么样子的??? 只执行一次: constructor.compo ...
- vue生命周期和react生命周期对比
一 vue的生命周期如下图所示(很清晰)初始化.编译.更新.销毁 二 vue生命周期的栗子 注意触发vue的created事件以后,this便指向vue实例,这点很重要 <!DOCTYPE ht ...
随机推荐
- Char、AnsiChar、WideChar、PChar、PAnsiChar、PWideChar 的用法
varc: Char; {Char 类型的取值范围是: #0..#255, 用十六进制表示是: #$0..#$FF}begin{用十进制方式赋值:}c := #65;ShowMessage(c); ...
- 在 VirtualBox 中 CentOS 网络设置
转自:本文发表于水景一页.永久链接:<http://cnzhx.net/blog/minimal-centos-in-virtualbox/>.转载请保留此信息及相应链接. 4. 设置¶ ...
- Quartz2D简介及基本线条绘制
* Quartz2D简介 1.什么是Quartz2D? 他是一个二维的绘图引擎,同时支持iOS和Mac系统 2.Quartz2D能完成的工作 画基本线条,绘制文字,图片,截图,自定义UIView. 3 ...
- C/C++获取数组的长度
C.C++中没有提供 直接获取数组长度的函数,对于存放字符串的字符数组提供了一个strlen函数获取长度,那么对于其他类型的数组如何获取他们的长度呢?其中一种方法是使 用sizeof(array) / ...
- 转 python range 用法
详细记录python的range()函数用法 使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的 ...
- [转]application windows are expected to have a root view controller错误
转载地址:http://blog.sina.com.cn/s/blog_6784e6ea01017jfy.html 最新更正:这个提示几乎不影响编译和运行,可以不用设置. 产生这个提示的操作: ...
- Power Network 分类: POJ 2015-07-29 13:55 3人阅读 评论(0) 收藏
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24867 Accepted: 12958 Descr ...
- UVa 213,World Finals 1991,信息解码
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- tinyhttpd服务器源码学习
下载地址:http://sourceforge.net/projects/tinyhttpd/ /* J. David's webserver */ /* This is a simple webse ...
- WPF调用office2010的ppt出错
各位热爱WPF编程小伙伴不可避免的会遇到将ppt嵌入到自己编写的软件,可是有时候会遇到错误,此错误值出现在卸载office2013并安装其他版本office时候会出现.这是由于某些机器上offic ...