網(wǎng)站首頁(yè) 編程語言 正文
1. 隔代組件傳值
1.1 props傳遞
傳遞方式:通過props屬性自上而下逐層傳遞
存在問題:層級(jí)過多導(dǎo)致傳值繁瑣
import React from 'react'
//父組件
class App extends React.Component{
render(){
return (
//1.傳遞value值
<Demo1 value="hello hello"/>
)
}
}
//中間組件
class Demo1 extends React.Component{
render(){
return (
//2.中間組件幫助傳遞value值
<Demo2 value={this.props.value} />
)
}
}
//子組件
class Demo2 extends React.Component{
render(){
return (
//3.接收value值
<h1>{this.props.value}</h1>
)
}
}
export default App
1.2 Context傳遞
傳遞方式:類似于全局變量
存在問題:組件復(fù)用性較差
使用方法:
- 創(chuàng)建Context(ValueContext可任意命名)
const ValueContext = React.createContext('') //默認(rèn)值為''
- 用ValueContext.Provider包裹組件樹中的根節(jié)點(diǎn),并傳遞
value
值<ValueContext.Provider value="hello hello">
- 此時(shí),該組件樹中的節(jié)點(diǎn)均能訪問到步驟2中根節(jié)點(diǎn)所傳遞的
value
值
3.1 指定contextType讀取當(dāng)前的ValueContextstatic contextType = ValueContext
3.2 讀取value值this.context
import React from 'react'
//1.創(chuàng)建一個(gè)Context
const ValueContext = React.createContext('') //默認(rèn)值為''
//父組件
class App extends React.Component{
render(){
return (
//2.用ValueContext.Provider包裹組件樹中的根節(jié)點(diǎn)
//傳遞value值為"hello hello"
//組件樹中的節(jié)點(diǎn)均能訪問到該value值
<ValueContext.Provider value="hello hello">
<Demo1/>
</ValueContext.Provider>
)
}
}
//中間組件
class Demo1 extends React.Component{
//指定contextType讀取當(dāng)前的ValueContext
static contextType = ValueContext
render(){
return (
//this.context即父組件中傳遞的"hello hello"
<div>
<h1>{this.context}</h1>
<Demo2/>
</div>
)
}
}
//子組件
class Demo2 extends React.Component{
//指定contextType讀取當(dāng)前的ValueContext
static contextType = ValueContext
render(){
return (
//this.context即父組件中傳遞的"hello hello"
<h2>{this.context}</h2>
)
}
}
export default App
效果圖:
原文鏈接:https://blog.csdn.net/SmallPig_Code/article/details/125708042
相關(guān)推薦
- 2022-05-03 C#面向?qū)ο笤O(shè)計(jì)原則之開閉原則_C#教程
- 2022-12-24 Go中函數(shù)的使用細(xì)節(jié)與注意事項(xiàng)詳解_Golang
- 2022-10-15 C語言中數(shù)據(jù)如何存儲(chǔ)進(jìn)內(nèi)存揭秘_C 語言
- 2022-06-28 C語言多線程開發(fā)中死鎖與讀寫鎖問題詳解_C 語言
- 2022-11-18 C語言手寫多級(jí)時(shí)間輪定時(shí)器_C 語言
- 2022-04-29 C#開發(fā)Winform控件之打開文件對(duì)話框OpenFileDialog類_C#教程
- 2022-07-13 Docker之Harbor私有倉(cāng)庫(kù)
- 2022-03-30 Flutter有狀態(tài)組件使用詳解_Android
- 最近更新
-
- 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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支