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

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 /> );

http://www.jsqmd.com/news/299392/

相关文章:

  • 从零开始:Gitee 仓库创建与本地项目纳管全流程详解
  • 法律服务效率提升的架构创新:AI应用架构师详解法律AI智能体微服务设计
  • 并行计算: CUDA 12.9 (支持 GPU 加速), OpenMP (多核 CPU)
  • Qt中connect()实现信号与槽连接这一核心机制
  • HDF5与CGNS文件格式详解
  • 资产管理系统如何让账实对齐变得简单又精准?
  • 本周学习总结
  • JDK动态代理和CGLIB代理的机制和选择
  • 洛谷 P1332 血色先锋队 题解
  • ClickHouse 25.12 版本发布说明
  • 什么才是真正影响性能的关键:一年来基准测试的经验教训
  • 大数据领域HBase的RegionServer管理技巧
  • ClickHouse 完成由 Dragoneer 领投的 4 亿美元 D 轮融资,加速其在分析与 AI 基础设施领域的扩张
  • 走向全栈:前后端状态认知差异与设计边界的深度探讨
  • Java毕设选题推荐:基于Java的小说三体科幻社区管理系统的设计与实现【附源码、mysql、文档、调试+代码讲解+全bao等】
  • 本周 GitHub 热门:更好用的MCP客户端和Coding创作视频,开源项目层出不穷!
  • 计算机Java毕设实战-基于springboot的三体科幻社区交流分享管理系统的设计与实现【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • Java毕设项目:基于springboot的三体科幻社区管理系统的设计与实现(源码+文档,讲解、调试运行,定制等)
  • 【课程设计/毕业设计】基于vue+springboot科幻社区管理系统springboot的三体科幻社区管理系统的设计与实现【附源码、数据库、万字文档】
  • 生成器跟容器还是不一样的,生成器可能有“江郎才尽”的那一天 - GLORY-TO-THE
  • ppo走中国象棋如果走到某一步3步之内必死会怎么样
  • 03.Python IDE / 编辑器选型指南:PyCharm/VS Code/IDLE 使用对比
  • 【已解决】浏览器出现 STATUS_STACK_BUFFER_OVERRUN 错误的原因及解决方法汇总
  • 5060laptop 显卡安装torch
  • 详细介绍:LLaMA大模型家族发展介绍
  • 模拟 TI C6678 多核并行加速的雷达目标检测与协同处理
  • selenium自动化测试工具实战项目(登录页面)
  • 字体反爬分析
  • JS加密算法
  • Leetcode 剑指 Offer II 159. 库存管理 III