引言

关于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. css分离思想

    CSS命名就应该最简单.最直接,直捣黄龙.没有HTML标签,没有层级,这些通通滚蛋,不要.为什么不要,有三大原因: 1. 限制重用 我们会使用层级(#test .test),会使用标签(ul.test ...

  2. while DEMO

    while DEMO #!/bin/bash #author:xiluhua #since: let v_count= " ] do ] then sleep echo $v_count l ...

  3. DBLINK的session无法关闭,报异常!

    ------解决方法-------------------------------------------------------- --ALTER SESSION alter session clo ...

  4. 为Docker容器配置固定IP

    当docker以桥接的方式启动容器时,容器内部的IP是经过DHCP获取的,例如:172.17.0.8/32,且每重启依次IP都会发生变动.某些特殊的情况下,需要容器内有自己固定的一个内部IP.我的实现 ...

  5. PHP编写的图片验证码类文件分享方法

    适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...

  6. PHP常用的数组相关处理函数

    [数组中常用的多种遍历方式] [for 和 foreach 略] [while() . list() .each() 组合循环遍历数组] each()函数 a. 需要一个数组作为参数 b. 返回来的也 ...

  7. 7、XML加强/Web开发/Tomcat

    1 XML加强 XML加强 1)Dom4j修改XML文档 写出xml文档: XMLWriter writer = new XMLWriter() writer.wrtite(doc); 增加: Doc ...

  8. SQL SERVER 数据库实用SQL语句

    --查看指定表的外键约束 select * from sysobjects where parent_obj in( select id from sysobjects where name='表名' ...

  9. Poj(2253),Dijkstra松弛条件的变形

    题目链接:http://poj.org/problem?id=2253 题意: 给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通 ...

  10. vilte/vowifi

    vendor/mediatek/proprietary/packages/services/Ims/src/com/mediatek/ims/ImsService.java ¦ ¦ ¦ ¦ ¦ ¦ v ...