什么是生命周期函数:在某一时刻组件会自动调用执行的函数。

import React,{ Component,Fragment } from 'react'
class Note extends Component{
//在组件创建的那一刻就会执行,不过是es6语法的,不能算生命周期函数
//Initialization初始化时在这里定义state,接收props
constructor(props){
super(props);
//当组件的state,props发生改变的时候render会重新执行
this.state={
inputValue:'',
list:[112]
}
} //组件第一次即将被挂载到页面的时候,自动执行
componentWillMount(){
console.log('componentWillMount');
} //组件组件第一次挂载到页面之后,自动被执行
componentDidMount(){
console.log('componentDidMount');
//在这里获取ajax数据,componentDidMount只执行一次
this.init(this.props);
}
init = (props)=>{
//存放ajax接口
} //一个组件要从父组件接受参数
////只要父组件的render函数被重新执行了,子组件的这个生命周期函数就会被执行
//////只要父组件的render函数被执行了,子组件的这个componentWillReceiveProps函数就会被执行
//如果这个组件第一次存在于父组件中,不会执行
//如果这个组件之前已经存在于父组件中,才会执行
//对于顶层组件不会执行这个生命周期函数
componentWillReceiveProps(){
console.log('child componentWillReceiveProps');
} //组件需要更新吗,返回的是一个Boolean值,如果返回true,componentWillUpdate、 render、componentDidUpdate 会执行,如果返回false不会执行componentWillUpdate、 render、componentDidUpdate函数
//组件跟新之前,他会被自动执行
//接下来props变化成什么样,接下来state会变化成什么样
shouldComponentUpdate(nextProps,nextState){
console.log('shoundComponentUpdate');
//接收的props不等于当前的props时会重新渲染,否则不会,提升了性能
if(nextProps.content !==this.props.content){
return true;
}
else{
return false;
}
} //当shouldComponentUpdate返回true时,在重新渲染之前(render)之前会被执行
//组件跟新之前,他会自动执行,但是他会在shouldComponentUpdate之后被执行
//如果shouldComponentUpdate返回true它才执行
//如果shouldComponentUpdate返回false就不会执行了
componentWillUpdate(){
console.log('componentWillUpdate');
} //当shouldComponentUpdate返回true时,在重新渲染之后(render)之后会被执行
componentDidUpdate(){
console.log('componentDidUpdate')
} //当这个组件即将被从页面中剔除的时候,会被执行
componentWillUnmount(){
console.log('componentWillUnmount');
} handelInput = (e)=>{
console.log(e.target.value);
//调用setState改变state的值就是更新组建的内容,render重新执行,用最新的数据渲染出模板
this.setState({inputValue:e.target.value});
}
addItem =()=>{
const {list,inputValue} = this.state;
this.setState({list:[...list,inputValue],inputValue:''})
}
removeItem = (index)=>{
console.log(index);
const {list,inputValue} = this.state;
let newList = list;
newList.splice(index,1);
this.setState({list:newList});
}
//数据(props 或者state)发生改变的时候,会被执行
//父组件的render执行后,子组件的render函数也会被执行
//render函数是组件中必须有的生命周期函数,因为这个组件是继承Copmonent组件,react这个组件内置默认了所有的生命周期函数,除了render没有内置。
render(){
console.log('render');
const {list} = this.state;
return (
<Fragment>
<input onChange={this.handelInput} value={this.state.inputValue}/>
<button onClick={this.addItem}>提交</button>
<ul>
{
list.map((item,index)=>{
return <li key={index} onClick={()=>this.removeItem(index)}>{item}</li>
})
}
</ul>
</Fragment>
)
} }
export default Note;
//在同一个方法中多次使用setState,setState会被合并执行,对于相同属性的设置会保留最后一次设置

2.React 生命周期函数的更多相关文章

  1. React生命周期函数详解

    React生命周期函数 生命周期函数是指在某一个周期自动执行的函数. React中的生命周期执行过程 以下是React中的常用的生命周期函数,按个部分中按照自动执行顺序列出,这几个过程可能存在同时进行 ...

  2. 十二、React 生命周期函数

    React生命周期函数: [官方文档]:https://reactjs.org/docs/react-component.html [定义]组件加载之前,组件加载完成,以及组件更新数据,组件销毁. 触 ...

  3. react生命周期函数使用箭头函数,导致mobx-react问题

    最近新人加入了项目,遇到了一个很奇怪的问题.mobx observable 属性,onChange的时候就是页面不会刷新. 试来试去,就是不知道什么原因,后来其他同事查到是因为componentWil ...

  4. react生命周期函数的应用-----1性能优化 2发ajax请求

    知识点1:每次render其实就会将jax的模板生成一个虚拟dom,跟上一个虚拟dom进行比对,通过diff算法找出不同,再更新到真实dom上去. 1性能优化 每次父组件render一次(除了第一次初 ...

  5. react 生命周期函数介绍

    constructor():构造函数 执行:组件加载钱最先调用一次,仅调用一次. 作用:定义状态机变量. 注意:第一个语句必须为super(), 否则会报错:'this' is not allowed ...

  6. react生命周期函数

      如图,可以把组件生命周期大致分为三个阶段: 第一阶段:是组件第一次绘制阶段,如图中的上面虚线框内,在这里完成了组件的加载和初始化: 第二阶段:是组件在运行和交互阶段,如图中左下角虚线框,这个阶段组 ...

  7. react 生命周期函数的一些心得体会

    一.理论 组件本质上是状态机,输入确定,输出一定确定 生命周期的三个阶段,三者时间是不固定的,只是在逻辑上的分类: 二.初始化阶段: getDefaultProps:获取实例的默认属性(即使没有生成实 ...

  8. 说一说我了解的react生命周期函数

    我了解的几个阶段 Mounting 挂载 Updating 更新 Unmounting 卸载 我说几个我常用的钩子函数 1.挂载阶段Mounting 1)constructor():函数构造器 执行次 ...

  9. react 生命周期函数

    (1)初始化阶段:getDefaultProps:获取实例的默认属性static propTypes 设置属性的类型componentWillMount:组件即将首次被装载.渲染到页面上render: ...

随机推荐

  1. dynamic类型

    dynamic类型在运行时做类型检查 可用于变量类型.方法参数和返回值类型 示例 dynamic person = new Student { Name = "张三", Age = ...

  2. 洛谷P4983 忘情 (WQS二分+斜率优化)

    题目链接 忘情水二分模板题,最优解对划分段数的导数满足单调性(原函数凸性)即可使用此方法. 详细题解洛谷里面就有,不啰嗦了. 二分的临界点让人有点头大... #include<bits/stdc ...

  3. flask调试模式

    想要启用调试模式,发现安装目前网上流行的两种方式均无法在Pycharm中打开调试模式. 1)直接在对象上设置 flask 更新到1.0以后 不支持使用debug =True 来开启调试模式了. Pri ...

  4. hivesql之 table名 with as 转储

    可能某个子查询在多个层级多个地方存在重复使用的情况,这个时候我们可以使用 with as 语句将其独立出来,极大提高SQL可读性,简化SQL~ 注:目前 oracle.sql server.hive等 ...

  5. Codeforces Round #587 (Div. 3) D. Swords

    链接: https://codeforces.com/contest/1216/problem/D 题意: There were n types of swords in the theater ba ...

  6. 题解 noip2019模拟赛Day1T3

    题面 运河计划 问题描述 水运在人类的交通运输史中一直扮演着重要的角色.借助河流.的便利,人们得以把大量的货物输送到天南海北不仅仅是自然界现成的河流,人工开凿的运河(如苏伊士运河.巴拿马运河.我国的京 ...

  7. oracle 分配表权限给用户的写法

    grant select on xxxx.xxxxx_TB to sb;grant select on xxxx.xxxxxx_tb to sb;

  8. [Luogu] 聪明的质监员

    https://www.luogu.org/problemnew/show/P1314 满足单调性 所以,二分W,进行检验 #include <iostream> #include < ...

  9. 【转载】C++ STL priority_queue用法

    priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为 ...

  10. python录音程序

    # -*- coding: utf-8 -*- # @Time : 18-10-16 下午12:20 # @Author : Felix Wang import pyaudio import nump ...