• React技术栈 --》组件生命周期和Vue拓展 ## Day6


    文章目录

    一、组件生命周期

    生命周期-概述

    挂载阶段

    钩子函数 constructor

    钩子函数 render

    钩子函数 componentDidMount

    更新阶段

    钩子函数 render

    钩子函数 componentDidUpdate

    卸载阶段

    钩子函数 componentWillUnmount

    总结

    生命周期的概念

    React组件生命周期的过程

    二、拓展Vue生命周期


    前情回顾:React技术栈 --》文件模块化和按钮绑定事件 ## Day5_亦世凡华、的博客-CSDN博客

    一、组件生命周期

    生命周期-概述

    组件的生命周期是指组件从被创建到挂载到页面中运行起来,在到组件不用时卸载的过程。注意:只有类组件才有生命周期 (类组件 实例化;函数组件 不需要实例化)

    Mounting(挂载):已插入真实 DOM

    Updating(更新):正在被重新渲染

    Unmounting(卸载):已移出真实 DOM

    挂载阶段

    举个例子,我们依次在控制台输出三个过程,查看是否与我们输出的循序一致

    1. import React from "react";
    2. class App extends React.Component{
    3. constructor(){
    4. super()
    5. console.log('constructor');
    6. }
    7. componentDidMount(){
    8. console.log('componentDidMount');
    9. }
    10. render(){
    11. console.log('render');
    12. return <div>
    13. this is div
    14. </div>
    15. }
    16. }
    17. export default App
    1. //1.导入包
    2. import React from 'react'
    3. import ReactDOM from 'react-dom'
    4. import App from '@/App.js'
    5. //3.将创建好的虚拟DOM渲染到页面上去
    6. ReactDOM.render(<div>
    7. <App></App>
    8. </div>,document.getElementById('app'))

    render和componentDidMount循序颠倒,说明我们不要以输出的循序为主,而是以它真正执行的顺序为主。

    钩子函数 constructor

    触发时机:创建组件时,最先执行,初始化的时候只执行一次。

    作用:1.初始化state 、2.创建Ref、3.使用bind解决this指向问题

    之前我们初始化state的时候会在构造器内书写,现在React也允许我们直接在外边书写

    钩子函数 render

    触发时机:每次组件渲染都会触发

    作用:渲染UI (注意:不能在里面调用setState)

    每次只要引起视图变化,我们的render都会执行。

    钩子函数 componentDidMount

    触发时机:组件挂载 (完成DOM渲染) 后执行,初始化的时候执行一次

    作用:1.发送网络请求、2.DOM操作

    更新阶段

    render和componentDidMount每次更新都会依次执行

    钩子函数 render

    触发时机:每次组件渲染都会触发

    作用:渲染UI (与 挂载阶段 是同一个render)

    钩子函数 componentDidUpdate

    触发时机:组件更新后 (DOM渲染完毕)

    作用:DOM操作,可以获取到更新后的DOM内容,不要直接调用setState

    卸载阶段

    钩子函数 componentWillUnmount

    触发时机:组件卸载 (从页面中消失)

    作用:执行清理工作 (比如:清理定时器等)

    1. import React from "react";
    2. class Test extends React.Component{
    3. componentWillUnmount(){
    4. console.log('componentWillUnmount');
    5. //清理定时器
    6. }
    7. render(){
    8. return <div>
    9. test
    10. </div>
    11. }
    12. }
    13. class App extends React.Component{
    14. constructor(){
    15. super()
    16. // this.state = {
    17. // }
    18. }
    19. state = {
    20. count:0,
    21. flag:true
    22. }
    23. clickHandler = () =>{
    24. this.setState({
    25. count: this.state.count+1,
    26. flag: !this.state.flag
    27. })
    28. }
    29. componentDidMount(){
    30. console.log('componentDidMount');
    31. //Ajax 类似于 mounted
    32. }
    33. componentDidUpdate(){
    34. console.log('componentDidUpdate');
    35. }
    36. render(){
    37. console.log('render');
    38. return <div>
    39. this is div
    40. {/* 通过一个数据状态的切换 让Test组件进行销毁重建 就会发生组件卸载 */}
    41. {this.state.flag ? <Test /> : null}
    42. <button onClick={this.clickHandler}>{this.state.count}</button>
    43. </div>
    44. }
    45. }
    46. export default App

    1. import React from "react";
    2. class Test extends React.Component{
    3. //如果数据是组件的状态需要去影响视图 定义到state中
    4. //如果我们需要的数据状态 不和视图绑定 定义成一个普通的实例属性就可以了
    5. //state中尽量保持精简
    6. timer = null
    7. componentDidMount(){
    8. this.timer = setInterval(()=>{
    9. console.log('定时器开启');
    10. },1000)
    11. }
    12. componentWillUnmount(){
    13. console.log('componentWillUnmount');
    14. clearInterval(this.timer)
    15. }
    16. render(){
    17. return <div>
    18. test
    19. </div>
    20. }
    21. }
    22. class App extends React.Component{
    23. constructor(){
    24. super()
    25. // this.state = {
    26. // }
    27. }
    28. state = {
    29. count:0,
    30. flag:true
    31. }
    32. clickHandler = () =>{
    33. this.setState({
    34. count: this.state.count+1,
    35. flag: !this.state.flag
    36. })
    37. }
    38. componentDidMount(){
    39. console.log('componentDidMount');
    40. //Ajax 类似于 mounted
    41. }
    42. componentDidUpdate(){
    43. console.log('componentDidUpdate');
    44. }
    45. render(){
    46. console.log('render');
    47. return <div>
    48. this is div
    49. {/* 通过一个数据状态的切换 让Test组件进行销毁重建 就会发生组件卸载 */}
    50. {this.state.flag ? <Test /> : null}
    51. <button onClick={this.clickHandler}>{this.state.count}</button>
    52. </div>
    53. }
    54. }
    55. export default App

    总结

    生命周期的概念

    每个组件的实例,从创建、到运行、直到销毁,在这个过程中,会触发一系列事件,这些事件就叫做组件的生命周期函数。

    React组件生命周期的过程

    1. //组件创建阶段 特点:一辈子只执行一次
    2. componentWillMount:
    3. render:
    4. componentDidMount:
    1. //组件运行阶段:按需,根据props属性或state状态的改变,有选择性的执行0到多次
    2. componentWillReceiveProps:
    3. shouldComponentUpdate:
    4. componentWillUpdate:
    5. render:
    6. componentDidUpdate:
    1. //组件销毁阶段
    2. componentWillUnmount:

    二、拓展Vue生命周期

    我们借助Vue的生命周期图浅解生命周期的概念和过程。

    原创不易,呜呜~,看到这还不点赞加收藏?

     

  • 相关阅读:
    ssh远程连接报错:WARNING: POSSIBLE DNS SPOOFING DETECTED(已解决)
    将VMware平台虚拟机瞬时恢复并在线迁移至深信服Sangfor平台
    C/C++ __builtin 超实用位运算函数总结
    JVM—类加载子系统
    数据恢复方法有哪些?如何恢复误删照片
    06_ElasticSearch:索引和文档的CURD
    【Java初阶】面向对象三大特性之多态
    .NET 7.0 重磅发布及资源汇总
    tkinter绘制组件(31)——支点标题
    [程序人生]常用的Linux命令简称与全称
  • 原文地址:https://blog.csdn.net/qq_53123067/article/details/125469928