網(wǎng)站首頁 編程語言 正文
react組件實例屬性state
state
- 有狀態(tài)state的組件稱作復(fù)雜組件,沒有狀態(tài)的組件稱為簡單組件
- 狀態(tài)里存儲數(shù)據(jù),數(shù)據(jù)的改變驅(qū)動頁面的展示
<script type="text/babel"> // 創(chuàng)建組件 class Weather extends React.Component { // 構(gòu)造器調(diào)用1次 constructor(props) { super(props); // 必須接收對象 this.state = { isHot: true }; // 左邊為實例對象,實例對象原本沒有changeWeather // 順著原型對象查找到changeWeather , 再由bind修改綁定的this // 最后左側(cè)多出了個函數(shù),并賦值為changeWeather this.changeWeather = this.changeWeather.bind(this); } // render調(diào)用 1+n次 初始1次,更新n次 render() { // 給事件指定回調(diào)函數(shù),不是賦值,不要有括號 // onClick中C要大寫 // 這里的changeWeather是實例的changeWeather return <h1 onClick={this.changeWeather} >今天天氣{this.state.isHot ? '炎熱' : '涼爽'}</h1> } // changeWeather放在Weather的原型對象上,供實例使用 // 由于changeWeather是onClick的回調(diào),所以不是通過實例調(diào)用的,而是直接調(diào)用的 // 類中的方法默認開啟了局部的嚴格模式 ,所以changeWeather中的this是undefined // 更新n次,調(diào)用n次 changeWeather() { // 狀態(tài)里的數(shù)據(jù)不能直接修改,以下就是直接修改 // 要借助內(nèi)置的api更改 // 以下這種寫法并未改變state // this.state.isHot = !this.state.isHot; // 要這樣更新,且更新是合并更新,未修改的不變 const isHot = this.state.isHot; this.setState({ isHot: !isHot }); } } // 渲染組件 const root = ReactDOM.createRoot(document.getElementById('test')); root.render(<Weather />); </script>
state簡寫
class Weather extends React.Component { state = { isHot: true }; render() { return <h1 onClick={this.changeWeather} >今天天氣{this.state.isHot ? '炎熱' : '涼爽'}</h1> } // 箭頭函數(shù)沒有this,但會找外層函數(shù)的this // 屬于自定義方法,算是賦值語句 changeWeather = () => { const isHot = this.state.isHot; this.setState({ isHot: !isHot }); } } const root = ReactDOM.createRoot(document.getElementById('test')); root.render(<Weather />); </script>
補充:react中的state詳解
state
理解:state是組件對象最重要的屬性,值是對象(可以包含多個key—value組合)
state中的值可以修改,修改的唯一方法是調(diào)用this.setState,每次修改以后,自動調(diào)用 this.render 方法,再次渲染組件。(也就是說直接
this.state.num=2
這樣直接修改值是無效的)
state在組件的構(gòu)造函數(shù)中賦值
class View extends Component{ constructor(){ super(); this.state={ num:10 } } //state={num:10} 直接做為實例屬性也可以 }
setState有兩種格式
- 傳一個對象
直接修改state的值
因為 this.props 和 this.state 可能會異步更新,所以不能依賴他們的值來更新下一個狀態(tài)。解決辦法讓 setState() 接收一個函數(shù)
this.setState({})
- 傳一個函數(shù)
接收兩個參數(shù)preState,props
更新前的state,更新后的props
修改state自身的值(例:state.num+4)
// 傳函數(shù)的兩種寫法 // 普通函數(shù) this.setState(function(state,props){ return {num:state.num--} }) // 箭頭函數(shù) this.setState((state,props)=>({num:state.num}))
遍歷state的值,循環(huán)渲染頁面
list(){ return this.state.lists.map((v,i)=>(<li key={i}>{v}</li>)) } //直接在dom中渲染即可(因為render會執(zhí)行兩次,所以要進行簡單的處理) <ul>{this.state.lists.length!==0:this.list():'';}</ul>
原文鏈接:https://www.cnblogs.com/ucbb/archive/2023/01/30/17073878.html
相關(guān)推薦
- 2023-01-21 詳解Go語言如何使用標準庫sort對切片進行排序_Golang
- 2022-09-05 Redis 數(shù)據(jù)刪除策略
- 2022-05-02 C語言遞歸實現(xiàn)歸并排序詳解_C 語言
- 2022-10-03 React在定時器中無法獲取狀態(tài)最新值的問題_React
- 2024-03-15 docker刪除、停止所有容器或鏡像
- 2022-08-17 Python字典查找數(shù)據(jù)的5個基礎(chǔ)操作方法_python
- 2022-03-15 SpringCloud 使用OpenFeign調(diào)用其他服務(wù)的時候feign.RetryableExc
- 2022-07-13 交換單鏈表第n和n+1個鏈點
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支