網(wǎng)站首頁 編程語言 正文
react組件實(shí)例屬性state
state
- 有狀態(tài)state的組件稱作復(fù)雜組件,沒有狀態(tài)的組件稱為簡單組件
- 狀態(tài)里存儲(chǔ)數(shù)據(jù),數(shù)據(jù)的改變驅(qū)動(dòng)頁面的展示
<script type="text/babel"> // 創(chuàng)建組件 class Weather extends React.Component { // 構(gòu)造器調(diào)用1次 constructor(props) { super(props); // 必須接收對象 this.state = { isHot: true }; // 左邊為實(shí)例對象,實(shí)例對象原本沒有changeWeather // 順著原型對象查找到changeWeather , 再由bind修改綁定的this // 最后左側(cè)多出了個(gè)函數(shù),并賦值為changeWeather this.changeWeather = this.changeWeather.bind(this); } // render調(diào)用 1+n次 初始1次,更新n次 render() { // 給事件指定回調(diào)函數(shù),不是賦值,不要有括號 // onClick中C要大寫 // 這里的changeWeather是實(shí)例的changeWeather return <h1 onClick={this.changeWeather} >今天天氣{this.state.isHot ? '炎熱' : '涼爽'}</h1> } // changeWeather放在Weather的原型對象上,供實(shí)例使用 // 由于changeWeather是onClick的回調(diào),所以不是通過實(shí)例調(diào)用的,而是直接調(diào)用的 // 類中的方法默認(rèn)開啟了局部的嚴(yán)格模式 ,所以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,但會(huì)找外層函數(shù)的this // 屬于自定義方法,算是賦值語句 changeWeather = () => { const isHot = this.state.isHot; this.setState({ isHot: !isHot }); } } const root = ReactDOM.createRoot(document.getElementById('test')); root.render(<Weather />); </script>
補(bǔ)充:react中的state詳解
state
理解:state是組件對象最重要的屬性,值是對象(可以包含多個(gè)key—value組合)
state中的值可以修改,修改的唯一方法是調(diào)用this.setState,每次修改以后,自動(dòng)調(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} 直接做為實(shí)例屬性也可以 }
setState有兩種格式
- 傳一個(gè)對象
直接修改state的值
因?yàn)?this.props 和 this.state 可能會(huì)異步更新,所以不能依賴他們的值來更新下一個(gè)狀態(tài)。解決辦法讓 setState() 接收一個(gè)函數(shù)
this.setState({})
- 傳一個(gè)函數(shù)
接收兩個(gè)參數(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中渲染即可(因?yàn)閞ender會(huì)執(zhí)行兩次,所以要進(jìn)行簡單的處理) <ul>{this.state.lists.length!==0:this.list():'';}</ul>
原文鏈接:https://www.cnblogs.com/ucbb/archive/2023/01/30/17073878.html
相關(guān)推薦
- 2023-01-20 Flask框架使用異常捕獲問題_python
- 2022-08-12 Windows?Server?修改遠(yuǎn)程桌面端口的實(shí)現(xiàn)_win服務(wù)器
- 2022-09-18 K8s實(shí)戰(zhàn)教程之容器和?Pods資源分配問題_云其它
- 2022-06-26 python中class類與方法的用法實(shí)例詳解_python
- 2022-07-26 msSQL中having的用處詳解_MsSql
- 2022-02-23 fatal error: mpi.h: No such file or directory // f
- 2023-07-08 vs編譯運(yùn)行報(bào)錯(cuò):未聲明的標(biāo)識符
- 2022-06-28 KVM基礎(chǔ)命令詳解_Kvm
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支