news 2026/5/31 1:22:24

React 组件状态(State)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
React 组件状态(State)

React 组件状态(State)

组件可以拥有状态(state),它是组件数据的私有部分,可以用来管理动态数据。

状态仅适用于类组件,或者使用 React 的 Hook 时可以在函数组件中使用。

React 把组件看成是一个状态机(State Machines)。通过与用户的交互,实现不同状态,然后渲染 UI,让用户界面和数据保持一致。

React 里,只需更新组件的 state,然后根据新的 state 重新渲染用户界面(不要操作 DOM)。

以下实例创建一个名称扩展为 React.Component 的 ES6 类,在 render() 方法中使用 this.state 来修改当前的时间。

添加一个类构造函数来初始化状态 this.state,类组件应始终使用 props 调用基础构造函数。


类组件中的状态管理

创建一个有状态的类组件:

Counter.js 文件

import React, { Component } from 'react';

class Counter extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}

increment = () => {
this.setState({ count: this.state.count + 1 });
}

render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.increment}>Increment</button>
</div>
);
}
}

export default Counter;

在 src/index.js 中渲染该组件:

实例

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Counter from './Counter';

const root = ReactDOM.createRoot(document.getElementById("root"));
// 渲染 Counter 组件
root.render(<Counter />);

函数组件中的状态管理(使用 Hook)

使用 React Hook 可以在函数组件中使用状态。最常用的 Hook 是 useState。

创建一个有状态的函数组件:

Counter.js 文件

import React, { useState } from 'react';

function Counter() {
const [count, setCount] = useState(0);

return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}

export default Counter;

在 src/index.js 中渲染该组件:

实例

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Counter from './Counter';

const root = ReactDOM.createRoot(document.getElementById("root"));
// 渲染 Counter 组件
root.render(<Counter />);


https://avg.163.com/topic/detail/9241300
https://avg.163.com/topic/detail/9241279
https://avg.163.com/topic/detail/9241299
https://avg.163.com/topic/detail/9241257
https://avg.163.com/topic/detail/9241281
https://avg.163.com/topic/detail/9241305
https://avg.163.com/topic/detail/9241263
https://avg.163.com/topic/detail/9241286
https://avg.163.com/topic/detail/9241290
https://avg.163.com/topic/detail/9241268
https://avg.163.com/topic/detail/9241295
https://avg.163.com/topic/detail/9241289
https://avg.163.com/topic/detail/9241307
https://avg.163.com/topic/detail/9241280
https://avg.163.com/topic/detail/9241302
https://avg.163.com/topic/detail/9241260
https://avg.163.com/topic/detail/9241285
https://avg.163.com/topic/detail/9241306
https://avg.163.com/topic/detail/9241270
https://avg.163.com/topic/detail/9241291
https://avg.163.com/topic/detail/9241262
https://avg.163.com/topic/detail/9241287
https://avg.163.com/topic/detail/9241273
https://avg.163.com/topic/detail/9241288
https://avg.163.com/topic/detail/9241274
https://avg.163.com/topic/detail/9241297
https://avg.163.com/topic/detail/9241265
https://avg.163.com/topic/detail/9241278
https://avg.163.com/topic/detail/9241298
https://avg.163.com/topic/detail/9241276
https://avg.163.com/topic/detail/9241294
https://avg.163.com/topic/detail/9241266
https://avg.163.com/topic/detail/9241292
https://avg.163.com/topic/detail/9241261
https://avg.163.com/topic/detail/9241284
https://avg.163.com/topic/detail/9241258
https://avg.163.com/topic/detail/9241283
https://avg.163.com/topic/detail/9241304
https://avg.163.com/topic/detail/9241267
https://avg.163.com/topic/detail/9241282
https://avg.163.com/topic/detail/9241108
https://avg.163.com/topic/detail/9241121
https://avg.163.com/topic/detail/9241112
https://avg.163.com/topic/detail/9241140
https://avg.163.com/topic/detail/9241122
https://avg.163.com/topic/detail/9241153
https://avg.163.com/topic/detail/9241138
https://avg.163.com/topic/detail/9241168
https://avg.163.com/topic/detail/9241147
https://avg.163.com/topic/detail/9241277
https://avg.163.com/topic/detail/9241155
https://avg.163.com/topic/detail/9241296
https://avg.163.com/topic/detail/9241170
https://avg.163.com/topic/detail/9241113
https://avg.163.com/topic/detail/9241176
https://avg.163.com/topic/detail/9241174
https://avg.163.com/topic/detail/9241182
https://avg.163.com/topic/detail/9241179
https://avg.163.com/topic/detail/9241264
https://avg.163.com/topic/detail/9241199
https://avg.163.com/topic/detail/9241185
https://avg.163.com/topic/detail/9241293
https://avg.163.com/topic/detail/9241124
https://avg.163.com/topic/detail/9241203
https://avg.163.com/topic/detail/9241196
https://avg.163.com/topic/detail/9241275
https://avg.163.com/topic/detail/9241211
https://avg.163.com/topic/detail/9241200
https://avg.163.com/topic/detail/9241301
https://avg.163.com/topic/detail/9241216
https://avg.163.com/topic/detail/9241131
https://avg.163.com/topic/detail/9241204
https://avg.163.com/topic/detail/9241109
https://avg.163.com/topic/detail/9241142
https://avg.163.com/topic/detail/9241213
https://avg.163.com/topic/detail/9241221
https://avg.163.com/topic/detail/9241123
https://avg.163.com/topic/detail/9241151
https://avg.163.com/topic/detail/9241218
https://avg.163.com/topic/detail/9241226
https://avg.163.com/topic/detail/9241132
https://avg.163.com/topic/detail/9241229
https://avg.163.com/topic/detail/9241141
https://avg.163.com/topic/detail/9241231
https://avg.163.com/topic/detail/9241150
https://avg.163.com/topic/detail/9241158
https://avg.163.com/topic/detail/9241160
https://avg.163.com/topic/detail/9241166
https://avg.163.com/topic/detail/9241167
https://avg.163.com/topic/detail/9241175
https://avg.163.com/topic/detail/9241114
https://avg.163.com/topic/detail/9241125
https://avg.163.com/topic/detail/9241133
https://avg.163.com/topic/detail/9241143
https://avg.163.com/topic/detail/9241152
https://avg.163.com/topic/detail/9241159
https://avg.163.com/topic/detail/9241169
https://avg.163.com/topic/detail/9241173
https://avg.163.com/topic/detail/9241177
https://avg.163.com/topic/detail/9241183
https://avg.163.com/topic/detail/9241195
https://avg.163.com/topic/detail/9241111
https://avg.163.com/topic/detail/9241120
https://avg.163.com/topic/detail/9241106
https://avg.163.com/topic/detail/9241139
https://avg.163.com/topic/detail/9241148
https://avg.163.com/topic/detail/9241156
https://avg.163.com/topic/detail/9241172
https://avg.163.com/topic/detail/9241105
https://avg.163.com/topic/detail/9241178
https://avg.163.com/topic/detail/9241118
https://avg.163.com/topic/detail/9241184
https://avg.163.com/topic/detail/9241137
https://avg.163.com/topic/detail/9241197
https://avg.163.com/topic/detail/9241107
https://avg.163.com/topic/detail/9241201
https://avg.163.com/topic/detail/9241110
https://avg.163.com/topic/detail/9241145
https://avg.163.com/topic/detail/9241117
https://avg.163.com/topic/detail/9241206
https://avg.163.com/topic/detail/9241119
https://avg.163.com/topic/detail/9241154
https://avg.163.com/topic/detail/9241126
https://avg.163.com/topic/detail/9241214
https://avg.163.com/topic/detail/9241136
https://avg.163.com/topic/detail/9241219
https://avg.163.com/topic/detail/9241149
https://avg.163.com/topic/detail/9241222
https://avg.163.com/topic/detail/9241157
https://avg.163.com/topic/detail/9241227
https://avg.163.com/topic/detail/9241230
https://avg.163.com/topic/detail/9241232
https://avg.163.com/topic/detail/9240046
https://avg.163.com/topic/detail/9240085
https://avg.163.com/topic/detail/9240116
https://avg.163.com/topic/detail/9240160
https://avg.163.com/topic/detail/9240195
https://avg.163.com/topic/detail/9240043
https://avg.163.com/topic/detail/9240086
https://avg.163.com/topic/detail/9240045
https://avg.163.com/topic/detail/9240118
https://avg.163.com/topic/detail/9240084
https://avg.163.com/topic/detail/9240146
https://avg.163.com/topic/detail/9240115
https://avg.163.com/topic/detail/9240174
https://avg.163.com/topic/detail/9240144
https://avg.163.com/topic/detail/9240044
https://avg.163.com/topic/detail/9240088
https://avg.163.com/topic/detail/9240124
https://avg.163.com/topic/detail/9240158
https://avg.163.com/topic/detail/9240193
https://avg.163.com/topic/detail/9234504
https://avg.163.com/topic/detail/9234511
https://avg.163.com/topic/detail/9234496
https://avg.163.com/topic/detail/9234503
https://avg.163.com/topic/detail/9234509
https://avg.163.com/topic/detail/9234493
https://avg.163.com/topic/detail/9234494
https://avg.163.com/topic/detail/9234499
https://avg.163.com/topic/detail/9234507
https://avg.163.com/topic/detail/9234491
https://avg.163.com/topic/detail/9234501
https://avg.163.com/topic/detail/9234508
https://avg.163.com/topic/detail/9234495
https://avg.163.com/topic/detail/9234506
https://avg.163.com/topic/detail/9234492
https://avg.163.com/topic/detail/9234486
https://avg.163.com/topic/detail/9234487
https://avg.163.com/topic/detail/9234488

实例

创建一个时间点实例来理解组件状态:

React 实例

class Clock extends React.Component { constructor(props) { super(props); this.state = {date: new Date()}; } render() { return ( <div> <h1>Hello, world!</h1> <h2>现在是 {this.state.date.toLocaleTimeString()}.</h2> </div> ); } } const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <Clock /> );


尝试一下 »

接下来,我们将使Clock设置自己的计时器并每秒更新一次。

将生命周期方法添加到类中

在具有许多组件的应用程序中,在销毁时释放组件所占用的资源非常重要。

每当 Clock 组件第一次加载到 DOM 中的时候,我们都想生成定时器,这在 React 中被称为挂载

同样,每当 Clock 生成的这个 DOM 被移除的时候,我们也会想要清除定时器,这在 React 中被称为卸载

我们可以在组件类上声明特殊的方法,当组件挂载或卸载时,来运行一些代码:

React 实例

class Clock extends React.Component { constructor(props) { super(props); this.state = {date: new Date()}; } componentDidMount() { this.timerID = setInterval( () => this.tick(), 1000 ); } componentWillUnmount() { clearInterval(this.timerID); } tick() { this.setState({ date: new Date() }); } render() { return ( <div> <h1>Hello, world!</h1> <h2>现在是 {this.state.date.toLocaleTimeString()}.</h2> </div> ); } } const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <Clock /> );

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/29 5:14:32

基于51单片机 智能鞋柜 语音识别 杀菌消毒无线控制DIY成品

目录 基于51单片机的智能鞋柜DIY方案硬件组成清单关键电路设计软件逻辑框架成品优化建议扩展功能实现 源码文档获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01; 基于51单片机的智能鞋柜DIY方案 核心功能 语音识别控制&#xff1a;通过LD3320等…

作者头像 李华
网站建设 2026/5/29 16:49:52

【基于STM32单片机甲烷煤气天然气报警厨房安全火灾报警火焰物联网 系统设计(实物+程序+原理图+其他资料)】

厨房&#xff0c;人们每天日常生活都会接触的地方&#xff0c;作为居民生我们每天都需要和厨房 打交道&#xff0c;厨房安全是我们日常生活中需要非常注重的地方。中国的厨房市场经过改 革开放三十年的发展&#xff0c;已经变得日趋成熟。21世纪以来&#xff0c;小区住宅的厨房…

作者头像 李华
网站建设 2026/5/22 15:54:50

PHP程序员学而思 = 思而学?

“学而思” ≠ “思而学” —— 这不是文字游戏&#xff0c;而是 认知科学与工程实践的根本分野。对 PHP 程序员而言&#xff0c;二者代表 两种截然不同的成长路径&#xff1a;前者是 输入驱动的被动学习&#xff0c;后者是 问题驱动的主动构建。 一、神经科学&#xff1a;大脑…

作者头像 李华
网站建设 2026/5/23 19:20:04

国货基金组织格奥尔基耶娃解读-万祥军| 世界经济论坛·国际科学院组织

国货基金组织格奥尔基耶娃解读-万祥军| 世界经济论坛国际科学院组织 国际货币基金组织&#xff08;IMF&#xff09;总裁克里斯塔利娜格奥尔基耶娃在达沃斯世界经济论坛2026年年会上的发言&#xff0c;引发了全球财经界的深度思考。这位保加利亚籍经济学家以"重建信任的宏…

作者头像 李华
网站建设 2026/5/22 13:34:57

AI大模型面试宝典:全面解析大模型技术,助你轻松应对各类面试问题

本文系统梳理了AI大模型开发技术的面试要点&#xff0c;涵盖增量预训练、知识蒸馏、推理加速等多个维度&#xff0c;并提供七阶段学习路线图及视频教程、电子书、面试题等资源&#xff0c;帮助程序员系统掌握大模型技术&#xff0c;提升面试竞争力。AI 大模型技术经过2025年的狂…

作者头像 李华