一、简介

在前面的第二篇博文中对组件的生命周期虽然做了一个大略介绍,但总感觉说的过于简单,毕竟生命周期是React组件的核心部分。在我们熟练使用React挂载和合成组件来创建应用表现层的过程中,针对数据异步或延时问题,只有充分利用组件的生命周期来把握框架载入和数据处理的时机,才能将组件性能发挥到合理水平,并提高应用程序的健壮性。基本来说,组件的生命周期可分为挂载生命周期和更新生命周期两大部分,它们都包括一系列方法,这些方法在组件渲染前后会被触发,事实上,render方法本身也是组件生命周期的一部分。当然,根据用户使用的是ES6类创建组件还是React.createClass创建组件,它们体现的生命周期有一点点的区别。使用React.createClass创建组件时,开发者可以默认在getDefalutProps和getInitialState函数中分别初始化属性和state,而使用ES6类创建组件时,这两个函数被取而代之是constructor构造函数,在构造函数内部可以获取默认属性并且设置state。完整的生命周期对比如图所示:

二、详解

1、挂载生命周期

包括方法有:constructor/getDefault/getInitialState、componentWillMount、render、componentDidMount、componentWillUnmout。

2、更新生命周期

包括方法有:componentWillReceiveProps、shouldComponentUpdate、componentWillUpdate、componentDidUpdate。

三、示例

1、React.createClass

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Hello React</title>
<script src="react/react.js"></script>
<script src="react/react-dom.js"></script>
<script src="react/browser.min.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/babel"> const targetNode = document.getElementById("container"); const Component = React.createClass({ getDefaultProps(){
console.log("---getDefaultProps---");
return {}
}, getInitialState(){
console.log("---getInitialState---");
return ({
count: 0
})
}, componentWillMount(){
console.log("---componentWillMount---")
}, render(){
console.log("---render---");
return (
<div>
<h1>{`${this.props.title} ${this.state.count}`}</h1>
</div>
)
}, componentDidMount(){
console.log("---componentDidMount---");
this.setProps({
title:"I AM XYQ! Welcome me, current count is"
})
},
/*
* 现象:父组件更新子组件的props,在子组件接收到新的props时,更新子组件的state,但是却没有重新渲染。
* 原因:官方说在该函数中调用 this.setState() 将不会引起第二次渲染。每次子组件接收到新的props,都会重新渲染一次,
* 除非你做了处理来阻止(比如使用:shouldComponentUpdate)。 但是你可以在这次渲染前,根据新的props更新state,
* 更新state也会触发一次重新渲染,但react基于性能考虑,只会渲染一次。
* */
componentWillReceiveProps(nextProps){
console.log("---componentWillReceiveProps---");
this.setState({
count: this.state.count + 1
})
}, shouldComponentUpdate(nextProps, nextState){
console.log("---shouldComponentUpdate---"+`count is ${nextState.count}`);
return true
}, componentWillUpdate(nextProps, nextState){
console.log("---componentWillUpdate---")
}, componentDidUpdate(nextProps, nextState){
console.log("---componentDidUpdate---");
ReactDOM.unmountComponentAtNode(targetNode); //卸载根组件
}, componentWillUnmount(){
console.log("---componentWillUnmout---");
} }); ReactDOM.render(
<Component/>,
targetNode
) </script>
</body>
</html>

结果与分析  【需要把ReactDOM.unmountComponentAtNode()方法注释掉,才会显示结果,不然组件就从DOM上卸载了】

2、ES6

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Hello React</title>
<script src="react/react.js"></script>
<script src="react/react-dom.js"></script>
<script src="react/browser.min.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/babel"> const targetNode = document.getElementById("container"); //父组件
class Parent extends React.Component { constructor(props){
super(props)
this.state = {title:""}
this.deleteComponent = this.deleteComponent.bind(this)
}; deleteComponent(){
ReactDOM.unmountComponentAtNode(targetNode); //卸载根组件
} render(){
return (
<div onClick={this.deleteComponent}>
<Children name={this.state.title}/>
</div>)
}; //父组件修改传递给子组件的属性值,子组件会触发componentWillReceiveProps函数
componentDidMount(){
this.setState({
title: "I AM XYQ! Welcome me, current count is"
})
};
} //子组件
class Children extends React.Component{ constructor(props){
super(props);
this.state = {count:0};
console.log("---constructor---")
}; componentWillMount(){
console.log("---componentWillMount---")
}; render(){
console.log("---render---");
return (
<h1>{`${this.props.name} ${this.state.count}`}</h1>
)
}; componentDidMount(){
console.log("---componentDidMount---");
}; //此处获取的nextProps就是父组件的state中的title属性
componentWillReceiveProps(nextProps){
console.log("---componentWillReceiveProps---");
this.setState({
count: this.state.count + 1
})
}; shouldComponentUpdate(nextProps, nextState){
console.log("---shouldComponentUpdate---"+`count is ${nextState.count}`);
return true
}; componentWillUpdate(nextProps, nextState){
console.log("---componentWillUpdate---")
}; componentDidUpdate(nextProps, nextState){
console.log("---componentDidUpdate---");
}; componentWillUnmount(){
console.log("---componentWillUnmout---");
}
} ReactDOM.render(
<Parent/>,
targetNode
) </script>
</body>
</html>

结果与分析  【点击deleteComponent()事件,组件就从DOM上卸载了】

React: React组件的生命周期的更多相关文章

  1. reactjs入门到实战(七)---- React的组件的生命周期

    React的组件的生命周期有三个状态分别是:挂载(生产组件示例化.准备挂载到页面.挂载到页面).更新(更新值.更新DOM).和卸载(卸载后). >>>其他     getInitia ...

  2. react native组件的生命周期

    react native组件的生命周期 一.当页面第一次加载时,会依次调用: constructor() componentWillMount(): 这个函数调用时机是在组件创建,并初始化了状态之后, ...

  3. Android React Native组件的生命周期及回调函数

    熟悉android的童鞋应该都清楚,android是有生命周期的,其很多组件也是有生命周期.今天小编和大家分享的React Native组件的生命周期,还不了解的童鞋,赶紧来围观吧 在android开 ...

  4. 【RN - 基础】之React Native组件的生命周期

    下图描述了React Native中组件的生命周期: 从上图中可以看到,React Native组件的生命周期可以分为初始化阶段.存在阶段和销毁阶段. 实例化阶段 实例化阶段是React Native ...

  5. React Native组件、生命周期及属性传值props详解

    创建组件的三种方式 第一种:通过ES6的方式创建 /** * 方式一 :ES6 */ export default class HelloComponent extends Component { r ...

  6. React:组件的生命周期

    在组件的整个生命周期中,随着该组件的props或者state发生改变,其DOM表现也会有相应的变化.一个组件就是一个状态机,对于特定地输入,它总返回一致的输出. 一个React组件的生命周期分为三个部 ...

  7. react教程 — 组件的生命周期 和 执行顺序

    一.组件执行的生命周期:                  参考  https://www.cnblogs.com/soyxiaobi/p/9559117.html  或  https://www.c ...

  8. React(三)组件的生命周期

    Component Specs and LifeCycle <div id="app"></div> <script src="bower_ ...

  9. React Native——组件的生命周期

    组件生命周期 上流程图描述了组件从创建.运行到销毁的整个过程,可以看到如果一个组件在被创建,从开始一直到运行会依次调用getDefaultProps到render这五个函数:在运行过程中,如果有属性和 ...

  10. React入门--------组件的生命周期

    Mounting/组件挂载相关: componentWillMount componentDidMount Updating/组件更新相关: componentWillReceiveProps sho ...

随机推荐

  1. VMware虚拟机安装Linux(CentOS)系统

    vmware workstation 虚拟机官方下载路径:https://www.vmware.com/cn/products/workstation-pro/workstation-pro-eval ...

  2. Python之如何修改运行的快捷键

    如果你在Pycharm中运行程序使用Ctrl+shift+F10快捷键,运行失败,使用Pycharm工具组,右键一下选择“Run+文件名称AAA”运行程序,直接运行成功的话,那么你就可以 更换自己的运 ...

  3. spring cloud详解

    1.Spring boot与Spring cloud 之间的关系 Spring boot 是 Spring 的一套快速配置脚手架,可以基于spring boot 快速开发单个微服务 ​ Spring ...

  4. python基础知识第二篇(字符串)

    基本数据类型 数字                  整形 int                             ---int                            将字符串 ...

  5. 【nginx+keepalived】nginx+keepalived搭建高可用

    一.结构及环境 1.1 环境介绍 操作系统:centos7 nginx+keepalived:106.53.73.200 master nginx+keepalived:182.254.184.102 ...

  6. leetcode第一题两数之和击败了 98.11% 的用户的答案(C++)

    虽然题目简单,但我这好不容易优化到前2%,感觉也值得分享给大家(方法比较偷机) 题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们 ...

  7. Ngnix:最通俗解读,Nginx是什么

    Nginx 同 Apache 一样都是一种 Web 服务器.基于 REST 架构风格,以统一资源描述符(Uniform Resources Identifier)URI 或者统一资源定位符(Unifo ...

  8. 学习SQL注入---1

    开始接触SQL注入了,最开始根据网上的思路做了两道注入的题,但对于SQL注入如何实现,怎么一个流程还是不理解.后来,在网上查找了很多资料,现在一点点去理解. 1.利用sqlmap注入的时候,不是所有页 ...

  9. View 的绘制过程

    配合Activity 从启动到布局绘制的简单分析 阅读 基本概念介绍 Activity:一个 Activity 是一个应用程序组件,提供一个屏幕,用户可以用来交互. View:所有视图控件的基类 Vi ...

  10. PHP 正则匹配h1的数据报错 preg_match(): Unknown modifier 'h' in

    问题: $str = "<h1>this is test msg</h1>"; $ruler = "/^<h1>(.*?)</h ...