当前位置: 首页 > news >正文

React 组件生命周期对比:Class vs. 函数式

在 React 中,Class 组件和函数式组件的生命周期存在一些差异。通过对 React 中 Class 组件和函数式组件的生命周期进行对比,详细探讨了它们在设计哲学、生命周期管理和开发技巧上的异同。全面了解 React 中两种组件类型的生命周期特点,以及如何灵活运用它们来构建现代化的 React 应用。
在这里插入图片描述

React Class 组件生命周期:

  1. constructor: 组件实例化时调用,用于初始化状态和绑定方法。

  2. componentDidMount: 组件挂载后调用,可以进行 DOM 操作或发起数据请求。

  3. componentDidUpdate: 组件更新后调用,用于处理更新前后的状态差异。

  4. componentWillUnmount: 组件即将被卸载时调用,用于清理定时器或取消订阅等操作。

React 函数式组件生命周期:

  1. useState 和 useEffect: 使用 useState 定义状态,使用 useEffect 进行副作用操作,相当于 Class 组件的 componentDidMount、componentDidUpdate 和 componentWillUnmount 的组合。

下面是一个简单的示例,演示了 Class 组件和函数式组件中生命周期的关系和区别:

// Class 组件
class ClassComponent extends React.Component {constructor(props) {super(props);this.state = {count: 0};}componentDidMount() {console.log("Component mounted");}componentDidUpdate(prevProps, prevState) {console.log("Component updated");}componentWillUnmount() {console.log("Component will unmount");}render() {return (<div><h2>Class Component</h2><p>Count: {this.state.count}</p><button onClick={() => this.setState({ count: this.state.count + 1 })}>Increment</button></div>);}
}// 函数式组件
import React, { useState, useEffect } from "react";function FunctionalComponent() {const [count, setCount] = useState(0);// 模拟 componentDidMountuseEffect(() => {console.log("Component mounted");// 清理函数,模拟 componentWillUnmountreturn () => {console.log("Component will unmount");};}, []);// 模拟 componentDidUpdateuseEffect(() => {console.log("Component updated");}, [count]); // 仅在 count 发生变化时执行return (<div><h2>Functional Component</h2><p>Count: {count}</p><button onClick={() => setCount(count + 1)}>Increment</button></div>);
}export default FunctionalComponent;// 父组件
function App() {const [showClassComponent, setShowClassComponent] = React.useState(true);return (<div>{showClassComponent ? <ClassComponent /> : <FunctionalComponent />}<button onClick={() => setShowClassComponent(!showClassComponent)}>Toggle Component</button></div>);
}ReactDOM.render(<App />, document.getElementById("root"));

在上面的示例中,Class 组件和函数式组件都实现了一个计数器,当点击按钮时,计数器会递增。在浏览器控制台中可以看到不同生命周期函数的输出。

总结一下:

  • Class 组件中的生命周期函数需要手动实现,而函数式组件使用 useEffect 来模拟生命周期行为。
  • 函数式组件中的 useEffect 可以模拟 componentDidMount、componentDidUpdate 和 componentWillUnmount,具体行为通过参数控制。
http://www.lryc.cn/news/338158.html

相关文章:

  • Ubuntu去除烦人的顶部【活动】按钮
  • Vue2(十五):replace属性、编程式路由导航、缓存路由组件、路由组件独有钩子、路由守卫、history与hash
  • 智慧污水井物联网远程监控案例
  • 程序员Java.vue,python前端后端爬虫开发资源分享
  • PCL:基于法线微分分割
  • 生产事故:线程管理不善诱发P0故障
  • WPF —— GDI画板
  • C++:基于范围的for循环
  • 引领智能互联时代,紫光展锐赋能百业创新发展
  • lv_micropython to download and building
  • 二叉树练习day.9
  • 2024年第十七届“认证杯”数学中国数学建模网络挑战赛B题思路
  • 【vue】slot 匿名插槽 / 具名插槽
  • FFmpeg: 自实现ijkplayer播放器-02环境搭建
  • Redis从入门到精通(十七)多级缓存(二)Lua语言入门、OpenResty集群的安装与使用
  • pytest常用钩子函数
  • .Net <% %>
  • 【C语言__编译和链接__复习篇2】
  • Jmeter —— 自动录制脚本
  • 使用python互相转换AVI、MP4、GIF格式视频文件
  • 11 Php学习:函数
  • 查询电脑用户名和组信息
  • 【Godot4.2】CanvasItem绘图函数全解析 - 9.绘制表格
  • 部署HDFS集群(完全分布式模式、hadoop用户控制集群、hadoop-3.3.4+安装包)
  • TCP协议简单总结
  • 【Qt 实现录音】
  • python:算法竞赛入门之一
  • 【大数据与云计算】虚拟机安装Linux
  • 从零开始编写一个cmake构建脚本
  • pringboot2集成swagger2出现guava的FluentIterable方法不存在