引言

关于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. Respond.js – 让 IE6-8 支持 CSS3 Media Query

    Respond.js 是一个快速.轻量的 polyfill,用于为 IE6-8 以及其它不支持 CSS3 Media Queries 的浏览器提供媒体查询的 min-width 和 max-width ...

  2. Index Condition Pushdown Optimization

    Index Condition Pushdown (ICP) is an optimization for the case where MySQL retrieves rows from a tab ...

  3. 在Ubuntu Kylin下安装QQ教程

    下载: 下载地址:http://www.ubuntukylin.com/application/show.php?lang=cn&id=279 下载后解压得到wine-qqintl文件夹,里面 ...

  4. 转:Spring AOP术语

    1.连接点(Joinpoint)       程序执行的某个特定位置:如类开始初始化前.类初始化后.类某个方法调用前.调用后.方法抛出异常后.这些代码中的特定点,称为“连接点”.Spring仅支持方法 ...

  5. mysql 段错误 (core dumped)

    一直使用好好的mysql命令,突然今天抽风,无论使用任何mysql选项都报“段错误 (core dumped)”,以为是mysqld程序出问题了,所以我尝试重启,因为我的环境上是多实例,用了mysql ...

  6. RTC系统

    http://blog.csdn.net/fanqipin/article/details/8089995 一. RTC及驱动简介 RTC即real time clock实时时钟,主要用于为操作系统提 ...

  7. 【转】Program Files (x86)文件夹是干什么的

    转载地址:http://leonax.net/p/2373/what-program-files-x86-folder-for/ 今天在微博上看到有人问这个问题.在装好64位的Windows之后,能在 ...

  8. 阿里巴巴Double分布式服务框架

    开发人员梁飞的blog : http://javatar.iteye.com/ 梁飞关于Double框架的专访: http://www.iteye.com/magazines/103 Double项目 ...

  9. HP QC IE11不支持( win7 64位 无法安装)解决方法

    QC IE11不支持( win7 64位 无法安装)解决方法 使用HP公司的QC做项目缺陷管理,发现IE浏览器只支持IE7,IE8.安装插件ALP_Platform_Loader提示64位无法安装,顿 ...

  10. msm8916 lcd 相关调试点指导

    主要代码:LINUX\android\kernel\arch\arm\boot\dts\qcom\dsi-panel-trust-hx8379c-fwvga-video.dtsiLINUX\andro ...