news 2026/4/17 19:57:21

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/4/18 5:31:56

你看到的人不舒服,就是不好,远离就行,或者不说话不表态:你是世界和核心,没有你,世界将不在存在

“驭人术”清单,把职场的脏底裤都扒下来了 目录 “驭人术”清单,把职场的脏底裤都扒下来了 你看到的人不舒服,就是不好,远离就行,或者不说话不表态 你是世界和核心,没有你,世界将不在存在 这些“驭人术”,本质是职场PUA的变种 真正的带人,从来不是“驭人” 打工人该怎…

作者头像 李华
网站建设 2026/4/18 5:35:50

stm32单片机智能浇花浇水土壤湿度光照检测远程控制系统设计成品

目录系统概述硬件组成软件设计功能特点应用场景注意事项源码文档获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;系统概述 STM32单片机智能浇花系统是一款基于土壤湿度、光照强度检测的自动化灌溉装置&#xff0c;支持远程控制。该系统通过传感…

作者头像 李华
网站建设 2026/4/18 0:06:36

学长亲荐!专科生毕业论文必备!8款一键生成论文工具TOP8测评

学长亲荐&#xff01;专科生毕业论文必备&#xff01;8款一键生成论文工具TOP8测评 2026年专科生毕业论文工具测评&#xff1a;从功能到体验的深度解析 随着人工智能技术的不断进步&#xff0c;越来越多的专科生开始借助AI工具辅助完成毕业论文写作。然而&#xff0c;面对市场上…

作者头像 李华
网站建设 2026/4/18 7:54:52

‍优必选Walker S2机器人2790专利-万祥军| 国研智库·中国国政研究

‍优必选Walker S2机器人2790专利-万祥军| 国研智库中国国政研究在深圳机器人谷的调研现场&#xff0c;国务院总理驻足观看的一幕成为科技界的标志性画面——优必选Walker S2机器人流畅完成自主换电与精密搬运的演示&#xff0c;其关节电机传出的细微嗡鸣声仿佛中国智能制造的新…

作者头像 李华