news 2026/7/15 4:33:35

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/7/1 9:57:21

模拟 TI C6678 多核并行加速的雷达目标检测与协同处理

1. 信号处理架构与参数定义 在 MATLAB 仿真开始前,需模拟硬件节点的分配: 数据结构:定义两个波束的回波数据矩阵。 计算节点模拟: FPGA 模块:模拟高速并行处理(运补、脉压、积累)。 DSP A/B 模块:模拟 8 核并行搜索逻辑。 中心控制逻辑:模拟多 DSP 结果融合与 3/5…

作者头像 李华
网站建设 2026/7/1 13:13:32

selenium自动化测试工具实战项目(登录页面)

介绍测试的系统&#xff1a;白月黑羽网站的测试系统(白月SMS系统)测试的功能&#xff1a;登录&#xff0c;退出登录。测试用例用例编号测试模块前置条件测试步骤预期结果实际结果Login_01登录功能已注册1.输入正确的用户名&#xff0c;输入错误的密码。2.点击登录登录不成功登录…

作者头像 李华
网站建设 2026/7/2 0:00:43

真正决定你成长速度的,从来不是你“经历了什么”,而是你“反思透了什么”

真正决定你成长速度的,从来不是你“经历了什么”,而是你“反思透了什么” 第一原则:前提你是充足的尝试,现在这个社会有可能你连经历的机会都没有,一个机会都得费劲的争取才行; 第二原则:反思是我们爱好的事情,让你感到不舒服的事情想都是浪费自己时间,例如职场扯皮…

作者头像 李华
网站建设 2026/7/14 21:14:59

支付宝支付 报错 invalid [default store dir]: /tmp/

支付宝SDK报错 invalid [default store dir]: /tmp/ 解决方法 这个错误主要是出现在windows上面&#xff0c;因为路径错误而导致的。 解决方法是在SDk里新建一个tmp文件夹&#xff0c;然后打开AopSdkl.php将18行中的 【define("AOP_SDK_WORK_DIR", "/tmp/&quo…

作者头像 李华
网站建设 2026/7/5 12:31:38

2025最新大模型面试经验汇总+全套学习资源,小白到大神的进阶之路

新大模型面试经验汇总全套学习资源&#xff0c;小白到大神的进阶之路 文章汇总了多家科技公司的大模型(LLM)相关面试经验&#xff0c;包括字节跳动、网易伏羲、好未来等公司的面试问题和回答。同时提供了一套系统的大模型学习路线图&#xff0c;从基础概念理解到API应用开发&a…

作者头像 李华