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

React--函数组件和类组件

React 中的函数组件和类组件是两种定义组件的方式,它们有以下主要区别:

1. 语法与定义方式

  • 函数组件: 是 JavaScript 函数,接收 props 作为参数,返回 JSX。

    const MyComponent = (props) => {return <div>Hello, {props.name}</div>;
    };
    
  • 类组件: 继承自 React.Component,必须定义 render() 方法返回 JSX。

    class MyComponent extends React.Component {render() {return <div>Hello, {this.props.name}</div>;}
    }
    

2. 状态管理

  • 函数组件: 最初无状态,需使用 Hooks(如 useState)管理状态。

    const Counter = () => {const [count, setCount] = useState(0);return <button onClick={() => setCount(count + 1)}>{count}</button>;
    };
    
  • 类组件: 通过 this.state 和 this.setState 管理状态。

    class Counter extends React.Component {state = { count: 0 };increment = () => {this.setState({ count: this.state.count + 1 });};render() {return <button onClick={this.increment}>{this.state.count}</button>;}
    }
    

3. 生命周期方法

  • 函数组件: 使用 useEffect Hook 替代生命周期方法。

    useEffect(() => {// 组件挂载后执行return () => {// 组件卸载前执行};
    }, []); // 依赖项为空数组时,等效于 componentDidMount 和 componentWillUnmount
    
  • 类组件: 有完整的生命周期方法(如 componentDidMount、componentDidUpdate、componentWillUnmount)。

    class MyComponent extends React.Component {componentDidMount() {// 组件挂载后执行}componentWillUnmount() {// 组件卸载前执行}
    }
    

4. 性能优化

  • 函数组件: 通过 React.memo 浅比较 props 避免重复渲染。

    const MyComponent = React.memo((props) => {return <div>{props.value}</div>;
    });
    
  • 类组件: 通过 shouldComponentUpdate 或继承 React.PureComponent 实现。

    class MyComponent extends React.PureComponent {// 自动浅比较 props 和 state
    }
    

5. 上下文与 refs

  • 函数组件: 使用 useContext 和 useRef Hooks。

    const value = useContext(MyContext);
    const ref = useRef(null);
    
  • 类组件: 通过 static contextType 或 Context.Consumer,以及 React.createRef()。

    static contextType = MyContext;
    ref = React.createRef();
    

6. 适用场景

  • 函数组件: 更简洁,适合无状态组件或逻辑简单的组件,是 React 的推荐写法。
  • 类组件: 适合复杂逻辑(如需要访问生命周期方法或使用 this),但逐渐被函数组件替代。

总结

特性函数组件类组件
语法函数 / 箭头函数继承 React.Component
状态管理Hooks(如 useState)this.state 和 setState
生命周期useEffect Hook完整生命周期方法
性能优化React.memoshouldComponentUpdate
适用场景无状态 / 简单逻辑复杂逻辑 / 生命周期依赖

现代 React 开发中,函数组件配合 Hooks 已成为主流,因为它们更简洁、可复用性更高,并且能更好地处理状态和副作用。

http://www.lryc.cn/news/2383391.html

相关文章:

  • Flask 路由装饰器:从 URL 到视图函数的优雅映射
  • DDoS防护实战——从基础配置到高防IP部署
  • aws平台s3存储桶夸域问题处理
  • HOT100(二叉树)
  • 【vue-text-highlight】在vue2的使用教程
  • pycharm无法正常调试问题
  • springboot3.4.5-springsecurity+session
  • 网络安全利器:蜜罐技术详解
  • Leetcode百题斩-哈希
  • MySQL替换瀚高数据库报错: TO_DAYS()不存在(APP)
  • EXIST与JOIN连表比较
  • 【Linux】利用多路转接epoll机制、ET模式,基于Reactor设计模式实现
  • 【jvm第7集】jvm调优工具(命令行工具)
  • react中运行 npm run dev 报错,提示vite.config.js出现错误 @esbuild/win32-x64
  • 鸿蒙UI开发——Builder与LocalBuilder对比
  • 关于光谱相机的灵敏度
  • Model 速通系列(一)nanoGPT
  • 微信小程序中,一个页面的数据改变了,怎么通知另一个页面也改变?
  • MySQL--day4--排序与分页
  • 自动化测试脚本点击运行后,打开Chrome很久??
  • iOS热更新技术要点与风险分析
  • 系统架构设计(十二):统一过程模型(RUP)
  • 系分论文《论软件系统安全分析和应用》
  • Mac安装redis
  • srs-7.0 支持obs推webrtc流
  • Babylon.js学习之路《七、用户交互:鼠标点击、拖拽与射线检测》
  • 星际争霸小程序:用Java实现策略模式的星际大战
  • 请问交换机和路由器的区别?vlan 和 VPN 是什么?
  • BERT 作为Transformer的Encoder 为什么采用可学习的位置编码
  • Python数据可视化高级实战之一——绘制GE矩阵图