React组件的生命周期

React中组件的生命周期函数,又叫钩子函数

React的生命周期分两类来说明

一、从组件的一般挂载与卸载的过程来分
二、从组件数据更新过程来划分

从组件的一般挂载与卸载的过程来分 :
  1. constructor ES6创建方式中的构造函数,在组件实例化的时候就会调用,创建组件的时候会默认添加constructor函数,如果你要添加state状态等的就要手动添加,不然可以省去。
  2. componentWillMount 组件渲染前调用的钩子函数,一般在这个钩子函数里面设置state或者请求数据给state赋值
  3. render渲染组件的钩子函数,可以说是每个组件都要用的钩子函数(无状态组件)直接return
  4. componentDidMount组件渲染之后调用的钩子函数。一般用处:获取真实的DOM元素节点(原生js操作DOM),进行DOM操作
  5. componentWillUnmount组件卸载的时候调用的钩子函数。一般作用在componentDidMount钩子函数中进行的事件绑定的移除。

code代码见下面

import React, {Component} from 'react';
export default class App extends Component {
    constructor(props) {
        super(props);
        console.time();
        console.log('我是constructor方法');
        console.timeEnd();
    }
    //组件渲染前
    componentWillMount() {
        console.time();
        console.log('我是componentWillMount方法');
        console.timeEnd();
    }
    render() {
        console.time();
        console.log('我是render方法')
        console.timeEnd();
        return (
            <div></div>
        )
    }
    //组件渲染之后
    componentDidMount() {
        console.time();
        console.log('我是componentDidMount方法');
        console.timeEnd();
        //console.log(dom);
    }
    componentWillUnmount(){
        console.log("组件卸载");
    }
}

在浏览器中运行结果

在浏览器控制台输出信息

从组件数据更新过程来划分(前提先抛开上面的根据过程的生命周期)
  1. componentWillReceiveProps(nextProps)获取父组件才会执行的钩子函数
  2. shouldComponentUpdate(nextProps,nextState)重新设置state的值的时候就会调用,在默认情况下,showComponentUpdate函数都返回true
  3. componentWillUpdate在组件渲染前调用
  4. componentDidUpdate在组件渲染之后调用

代码详见

import React, {Component} from 'react';
import {getJson} from './Utils';
export default class App extends Component {
    constructor(props) {
        super(props);
        console.time();
        console.log('我是constructor方法');
        console.timeEnd();
        this.state= {
            data:''
        }
    }
    //组件挂载之前的
    componentWillMount() {
        console.time();
        console.log('我是componentWillMount方法');
        console.timeEnd();
        //getJson是自己封装的fetch请求的数据方式
        getJson('http://www.xxxxx.com/mobile.php?c=Product&a=category',(response)=>{
            let {errmsg,data} = response;
            if(errmsg == '成功'){
                this.setState({
                    data:data
                })
            }
        })
    }
    //父组件更新props的时候子组件会优先shouldComponentUpdate前调用componentWillReceiveProps钩子函数
    componentWillReceiveProps(nextProps){
        console.log(`我是componentWillReceiveProps函数--${nextProps}`);
        return true;
    }
    //setState后都会调用shouldComponentUpdate判断是否需要重新
    shouldComponentUpdate(nextProps,nextState){
        console.log(`我是shouldComponentUpdate函数--${nextProps}---${nextState}`);
        //console.log(JSON.stringify(nextProps))
        return true;
    }
    //shouldComponentUpdate返回true或者调用forceUpdate之后
    componentWillUpdate(nextProps,nextState){
        console.log(`我是componentWillUpdate函数--${nextProps}---${nextState}`);
        return true;
    }
    componentDidUpdate(nextProps,nextState){
        console.log(`我是componentDidUpdate函数--${nextProps}---${nextState}`);
        return true;
    }
    render() {
        console.time();
        console.log('我是render方法')
        console.timeEnd();
        return (
            <div></div>
        )
    }
}

运行结果如下图

代码效果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水痕01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值