react生命周期的三个过程是什么

react生命周期的三个过程:1、挂载期,也叫实例化期,是一个组件实例初次被创建的过程;2、更新期,也被称为存在期,是组件在创建之后再次渲染的过程;3、卸载期,也被称为销毁期,是组件在使用完后被销毁的过程。

react生命周期的三个过程是什么

本教程操作环境:Windows10系统、react17.0.1版、Dell G3电脑。

react生命周期的三个过程是什么

React的生命周期从广义上分为三个阶段:挂载、渲染、卸载

从出生到成长,最后到死亡,这个过程的时间可以理解为生命周期。React的生命周期同理也是这么一个过程。

React的生命周期分为三个阶段:挂载期(也叫实例化期)、更新期(也叫存在期)、卸载期(也叫销毁期)。在每个周期中React都提供了一些钩子函数。

生命周期的描述如下:

挂载期:一个组件实例初次北创建的过程。

更新期:组件在创建后再次渲染的过程。

卸载期:组件在使用完后被销毁的过程。

组件的挂载:

组件在首次创建后,进行第一次的渲染为挂载期。挂载期有的一些方法会被依次触发,列举如下:

constructor(构造函数,初始化状态值)getInitialState(设置状态机)getDefaultProps(获取默认的props)UNSAFE_componentWillMount(首次渲染前执行)render(渲染组件)componentDidMount(render渲染之后执行的操作)

//组件挂载import React from 'react';import ReactDOM from 'react-dom';class HelloWorld extends React.Component{    constructor(props) {        super(props);        console.log("1,构造函数");        this.state={};        console.log("2,设置状态机");    }    static defaultProps={        name:"React",    }    UNSAFE_componentWillMount(nextProps, nextState, nextContext) {        console.log("3,完成首次渲染前调用");    }    render() {        console.log("4,组件进行渲染");        return (            

{this.props.name}

) } componentDidMount() { console.log("5,componentDidMount render渲染后的操作") }}ReactDOM.render(, document.getElementById('root'));

登录后复制

在这里插入图片描述

组件的更新:
组件更新,指的是在组件初次渲染后,进行了组件状态的改变。React在生命周期中的更新过程包括以下几个方法:

UNSAFE_componentWillReceiveProps :当父组件更新子组件state时,该方法会被调用。shouldComponentUpdate : 该方法决定组件state或props的改变是否需要重新渲染组件。UNSAFE_componentWillUpdate : 在组件接受新的state或者props时,即将进行重新渲染前调用该方法,和UNSAFE_componentWillMount方法类似。componentDidUpdate : 在组件重新渲染后调用该方法,和componentDidMount方法类似。

//组件更新class HelloWorldFather extends React.Component{    constructor(props) {        super(props);        this.updateChildProps=this.updateChildProps.bind(this);        this.state={  //初始化父组件            name:"React"        }    }    updateChildProps(){  //更新父组件state        this.setState({            name:"Vue"        })    }    render() {        return (            

{/*父组件的state传递给子组件*/}

) }}class HelloWorld extends React.Component{ constructor(props) { super(props); console.log("1,构造函数"); console.log("2,设置状态机") } UNSAFE_componentWillMount() { console.log("3,完成首次渲染前调用"); } UNSAFE_componentWillReceiveProps(nextProps, nextContext) { console.log("6,父组件更新子组件时调用该方法"); } shouldComponentUpdate(nextProps, nextState, nextContext) { console.log("7,决定组件props或者state的改变是否需要重新进行渲染"); return true; } UNSAFE_componentWillUpdate(nextProps, nextState, nextContext) { console.log("8,当接收到新的props或state时,调用该方法"); } render() { console.log("4,组件进行渲染"); return (

{this.props.name}

) } componentDidMount() { console.log("5,componentDidMount render后的操作"); } componentDidUpdate(prevProps, prevState, snapshot) { console.log("9,组件被重新选然后调用该方法"); }}ReactDOM.render(,document.getElementById("root"));

登录后复制

在这里插入图片描述
点击“更新子组件props”后:
在这里插入图片描述

组件的卸载:
生命周期的最后一个过程为组件卸载期,也称为组件销毁期。该过程主要涉及一个 方法,即componentWillUnmount,当组件从DOM树删除的时候调用该方法。

//组件卸载class HelloWorldFather extends React.Component{    constructor(props) {        super(props);        this.updateChildProps=this.updateChildProps.bind(this);        this.state={  //初始化父组件            name:"React"        }    }    updateChildProps(){  //更新父组件state        this.setState({            name:"Vue"        })    }    render() {        return (            

{/*父组件的state传递给子组件*/}

) }}class HelloWorld extends React.Component{ constructor(props) { super(props); console.log("1,构造函数"); console.log("2,设置状态机") } UNSAFE_componentWillMount() { console.log("3,完成首次渲染前调用"); } UNSAFE_componentWillReceiveProps(nextProps, nextContext) { console.log("6,父组件更新子组件时调用该方法"); } shouldComponentUpdate(nextProps, nextState, nextContext) { console.log("7,决定组件props或者state的改变是否需要重新进行渲染"); return true; } UNSAFE_componentWillUpdate(nextProps, nextState, nextContext) { console.log("8,当接收到新的props或state时,调用该方法"); } delComponent(){ //添加卸载方法 ReactDOM.unmountComponentAtNode(document.getElementById("root")); } render() { console.log("4,组件进行渲染"); return (

{this.props.name}

{/*声明卸载按钮*/}

) } componentDidMount() { console.log("5,componentDidMount render后的操作"); } componentDidUpdate(prevProps, prevState, snapshot) { console.log("9,组件被重新选然后调用该方法"); } componentWillUnmount() { //组件卸载后执行 console.log("10,组件已被卸载"); }}ReactDOM.render(,document.getElementById("root"));

登录后复制

在这里插入图片描述
点击卸载按钮后:
在这里插入图片描述

总览组件生命周期:
在这里插入图片描述

【相关推荐:javascript视频教程、web前端】

以上就是react生命周期的三个过程是什么的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2928395.html

(0)
上一篇 2025年3月11日 19:36:03
下一篇 2025年3月11日 19:36:15

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • react中map方法怎么用

    在react中,map方法用于遍历和显示组件的类似对象列表;map方法并不是react特有的,可以在任何数组上调用标准的JavaScript函数,map方法通过对调用数组的每个元素调用提供的函数来创建数组。 本教程操作环境:Windows1…

    2025年3月11日
    200
  • 什么是react?什么是Ant Design?

    react是Facebook推出的一个用来构建用户界面的JavaScript开发框架,主要用于构建UI,可使创建交互式UI变得轻而易举。Ant Design是阿里蚂蚁金服团队基于React开发的ui组件,主要用于中后台系统的使用;它它使用T…

    2025年3月11日 编程技术
    200
  • 什么是react条件渲染

    在react中,条件渲染是指在指定条件下进行渲染,如果不满足条件则不进行渲染;即界面的内容会根据不同的情况显示不同的内容,或者决定是否渲染某部分内容。react条件渲染的方式:1、条件判断语句,适合逻辑较多的情况;2、三元运算符,适合逻辑比…

    2025年3月11日 编程技术
    200
  • react中什么是父子组件

    在react组件的相互调用中,把调用者称为父组件,被调用者称为子组件。父子组件间可以传值:1、父组件向子组件传值时,先将需要传递的值传递给子组件,然后在子组件中,使用props来接收父组件传递过来的值;2、子组件向父组件传值时,需要通过触发…

    2025年3月11日 编程技术
    200
  • react新旧生命周期的区别是什么

    react新旧生命周期的区别:1、新生命周期中去掉了三个will钩子,分别为componentWillMount、componentWillReceiveProps、componentWillUpdate;2、新生命周期中新增了两个钩子,分…

    2025年3月11日
    200
  • react有哪些插件

    react插件有:1、Redux,提供可预测化的状态管理;2、MobX,通过函数响应式编程使得状态管理变得简单和可扩展;3、Redux Thunk,Redux的异步处理中间件;4、Dva,一个基于redux和redux-saga的数据流方案…

    2025年3月11日
    200
  • vue与react属于什么框架

    vue与react属于JavaScript框架。vue是一套用于构建用户界面的渐进式JavaScript框架,Vue的核心库只关注视图层,并且非常容易学习,非常容易与其它库或已有项目整合。react是一个用来构建用户界面的JavaScrip…

    2025年3月11日
    200
  • react组件首字母必须大写吗

    react组件首字母必须大写;因为React根据首字母是否大写来区分是react组件还是dom元素。React中使用JSX语法,但浏览器无法识别JSX语法,需通过babel对JSX语法进行转义;而如果组件的首字母为小写时,其会被认定为原生D…

    2025年3月11日 编程技术
    200
  • react用什么ui组件库

    react可用ui组件库有:1、Material-UI,有用于布局、表单、导航、数据显示和许多其他小部件的组件;2、Ant Design,有60多个组件,带有可预测的静态类型;3、Blueprint,有40多个组件;4、React Boot…

    2025年3月11日 编程技术
    200
  • react为什么要用合成事件

    react使用合成事件主要有三个目的:1、进行浏览器兼容,实现更好的跨平台;React提供的合成事件可用来抹平不同浏览器事件对象之间的差异,将不同平台事件模拟合成事件。2、避免垃圾回收;React事件对象不会被释放掉,而是存放进一个数组中,…

    2025年3月11日 编程技术
    200

发表回复

登录后才能评论