陕西省建设银行分行互联网互联网站浏览器微信登录入口
2026/4/6 11:20:13 网站建设 项目流程
陕西省建设银行分行互联网互联网站,浏览器微信登录入口,免费智能seo收录工具,青岛做网站企业React 组件状态(State) 组件可以拥有状态#xff08;state#xff09;#xff0c;它是组件数据的私有部分#xff0c;可以用来管理动态数据。 状态仅适用于类组件#xff0c;或者使用 React 的 Hook 时可以在函数组件中使用。 React 把组件看成是一个状态机#xff08…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 (divpCount: {this.state.count}/pbutton 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 (divpCount: {count}/pbutton 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/9241300https://avg.163.com/topic/detail/9241279https://avg.163.com/topic/detail/9241299https://avg.163.com/topic/detail/9241257https://avg.163.com/topic/detail/9241281https://avg.163.com/topic/detail/9241305https://avg.163.com/topic/detail/9241263https://avg.163.com/topic/detail/9241286https://avg.163.com/topic/detail/9241290https://avg.163.com/topic/detail/9241268https://avg.163.com/topic/detail/9241295https://avg.163.com/topic/detail/9241289https://avg.163.com/topic/detail/9241307https://avg.163.com/topic/detail/9241280https://avg.163.com/topic/detail/9241302https://avg.163.com/topic/detail/9241260https://avg.163.com/topic/detail/9241285https://avg.163.com/topic/detail/9241306https://avg.163.com/topic/detail/9241270https://avg.163.com/topic/detail/9241291https://avg.163.com/topic/detail/9241262https://avg.163.com/topic/detail/9241287https://avg.163.com/topic/detail/9241273https://avg.163.com/topic/detail/9241288https://avg.163.com/topic/detail/9241274https://avg.163.com/topic/detail/9241297https://avg.163.com/topic/detail/9241265https://avg.163.com/topic/detail/9241278https://avg.163.com/topic/detail/9241298https://avg.163.com/topic/detail/9241276https://avg.163.com/topic/detail/9241294https://avg.163.com/topic/detail/9241266https://avg.163.com/topic/detail/9241292https://avg.163.com/topic/detail/9241261https://avg.163.com/topic/detail/9241284https://avg.163.com/topic/detail/9241258https://avg.163.com/topic/detail/9241283https://avg.163.com/topic/detail/9241304https://avg.163.com/topic/detail/9241267https://avg.163.com/topic/detail/9241282https://avg.163.com/topic/detail/9241108https://avg.163.com/topic/detail/9241121https://avg.163.com/topic/detail/9241112https://avg.163.com/topic/detail/9241140https://avg.163.com/topic/detail/9241122https://avg.163.com/topic/detail/9241153https://avg.163.com/topic/detail/9241138https://avg.163.com/topic/detail/9241168https://avg.163.com/topic/detail/9241147https://avg.163.com/topic/detail/9241277https://avg.163.com/topic/detail/9241155https://avg.163.com/topic/detail/9241296https://avg.163.com/topic/detail/9241170https://avg.163.com/topic/detail/9241113https://avg.163.com/topic/detail/9241176https://avg.163.com/topic/detail/9241174https://avg.163.com/topic/detail/9241182https://avg.163.com/topic/detail/9241179https://avg.163.com/topic/detail/9241264https://avg.163.com/topic/detail/9241199https://avg.163.com/topic/detail/9241185https://avg.163.com/topic/detail/9241293https://avg.163.com/topic/detail/9241124https://avg.163.com/topic/detail/9241203https://avg.163.com/topic/detail/9241196https://avg.163.com/topic/detail/9241275https://avg.163.com/topic/detail/9241211https://avg.163.com/topic/detail/9241200https://avg.163.com/topic/detail/9241301https://avg.163.com/topic/detail/9241216https://avg.163.com/topic/detail/9241131https://avg.163.com/topic/detail/9241204https://avg.163.com/topic/detail/9241109https://avg.163.com/topic/detail/9241142https://avg.163.com/topic/detail/9241213https://avg.163.com/topic/detail/9241221https://avg.163.com/topic/detail/9241123https://avg.163.com/topic/detail/9241151https://avg.163.com/topic/detail/9241218https://avg.163.com/topic/detail/9241226https://avg.163.com/topic/detail/9241132https://avg.163.com/topic/detail/9241229https://avg.163.com/topic/detail/9241141https://avg.163.com/topic/detail/9241231https://avg.163.com/topic/detail/9241150https://avg.163.com/topic/detail/9241158https://avg.163.com/topic/detail/9241160https://avg.163.com/topic/detail/9241166https://avg.163.com/topic/detail/9241167https://avg.163.com/topic/detail/9241175https://avg.163.com/topic/detail/9241114https://avg.163.com/topic/detail/9241125https://avg.163.com/topic/detail/9241133https://avg.163.com/topic/detail/9241143https://avg.163.com/topic/detail/9241152https://avg.163.com/topic/detail/9241159https://avg.163.com/topic/detail/9241169https://avg.163.com/topic/detail/9241173https://avg.163.com/topic/detail/9241177https://avg.163.com/topic/detail/9241183https://avg.163.com/topic/detail/9241195https://avg.163.com/topic/detail/9241111https://avg.163.com/topic/detail/9241120https://avg.163.com/topic/detail/9241106https://avg.163.com/topic/detail/9241139https://avg.163.com/topic/detail/9241148https://avg.163.com/topic/detail/9241156https://avg.163.com/topic/detail/9241172https://avg.163.com/topic/detail/9241105https://avg.163.com/topic/detail/9241178https://avg.163.com/topic/detail/9241118https://avg.163.com/topic/detail/9241184https://avg.163.com/topic/detail/9241137https://avg.163.com/topic/detail/9241197https://avg.163.com/topic/detail/9241107https://avg.163.com/topic/detail/9241201https://avg.163.com/topic/detail/9241110https://avg.163.com/topic/detail/9241145https://avg.163.com/topic/detail/9241117https://avg.163.com/topic/detail/9241206https://avg.163.com/topic/detail/9241119https://avg.163.com/topic/detail/9241154https://avg.163.com/topic/detail/9241126https://avg.163.com/topic/detail/9241214https://avg.163.com/topic/detail/9241136https://avg.163.com/topic/detail/9241219https://avg.163.com/topic/detail/9241149https://avg.163.com/topic/detail/9241222https://avg.163.com/topic/detail/9241157https://avg.163.com/topic/detail/9241227https://avg.163.com/topic/detail/9241230https://avg.163.com/topic/detail/9241232https://avg.163.com/topic/detail/9240046https://avg.163.com/topic/detail/9240085https://avg.163.com/topic/detail/9240116https://avg.163.com/topic/detail/9240160https://avg.163.com/topic/detail/9240195https://avg.163.com/topic/detail/9240043https://avg.163.com/topic/detail/9240086https://avg.163.com/topic/detail/9240045https://avg.163.com/topic/detail/9240118https://avg.163.com/topic/detail/9240084https://avg.163.com/topic/detail/9240146https://avg.163.com/topic/detail/9240115https://avg.163.com/topic/detail/9240174https://avg.163.com/topic/detail/9240144https://avg.163.com/topic/detail/9240044https://avg.163.com/topic/detail/9240088https://avg.163.com/topic/detail/9240124https://avg.163.com/topic/detail/9240158https://avg.163.com/topic/detail/9240193https://avg.163.com/topic/detail/9234504https://avg.163.com/topic/detail/9234511https://avg.163.com/topic/detail/9234496https://avg.163.com/topic/detail/9234503https://avg.163.com/topic/detail/9234509https://avg.163.com/topic/detail/9234493https://avg.163.com/topic/detail/9234494https://avg.163.com/topic/detail/9234499https://avg.163.com/topic/detail/9234507https://avg.163.com/topic/detail/9234491https://avg.163.com/topic/detail/9234501https://avg.163.com/topic/detail/9234508https://avg.163.com/topic/detail/9234495https://avg.163.com/topic/detail/9234506https://avg.163.com/topic/detail/9234492https://avg.163.com/topic/detail/9234486https://avg.163.com/topic/detail/9234487https://avg.163.com/topic/detail/9234488实例创建一个时间点实例来理解组件状态React 实例class Clock extends React.Component { constructor(props) { super(props); this.state {date: new Date()}; } render() { return ( div h1Hello, 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 h1Hello, world!/h1 h2现在是 {this.state.date.toLocaleTimeString()}./h2 /div ); } } const root ReactDOM.createRoot(document.getElementById(root)); root.render( Clock / );

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询