網站首頁 編程語言 正文
Context/Provider/Consumer傳參使用
react context這個api很少用到,所以一直不太清楚如何使用,最近在看antd的項目源碼時,發現在組件中有類似Template.Comsumer的寫法,一時沒反應過來,本著碰到不懂得都要追根究底的原則,下面好好學習一下,Context這個api的使用
Context
作用
上下文(Context) 提供了一種通過組件樹傳遞數據的方法,無需在每個級別手動傳遞 props 屬性。
但是我們現在通用的傳遞數據的做法是使用redux,mobx,應為這些數據管理插件,能更好的對數據做管理已經更新 ,而使用Context只能將部分數據傳入,且使用起來比較麻煩,容易造成混亂.
所以一般情況下我們不要使用Context,雖然不太用到但是還是需要學習的,指不定哪天就轉正了
如何使用
這里直接就用官方的例子來解釋,自己懶得寫例子了 = ) !
//創建一個Context?
//light就是傳入的默認值 最好傳入默認值因為第一次渲染的時候沒有默認值可能會導致錯誤
const ThemeContext = React.createContext('light');
class App extends React.Component {
? render() {
? ? return (
? ? ? ? //使用Provider設置dark值
? ? ? <ThemeContext.Provider value="dark">
? ? ? ? <Toolbar />
? ? ? </ThemeContext.Provider>
? ? );
}
//中間組件
function Toolbar(props) {
? return (
? ? <div>
? ? ? <ThemedButton />
? ? </div>
? );
}
class ThemedButton extends React.Component {
//注冊context自動獲取當前組件最近的Provider,獲取到context的值 ,這種寫法是不直接指定context的寫法
//可以為類上的 contextType 屬性分配由 React.createContext() 創建的 Context 對象。 這允許您使用this.context 使用該 Context 類型 的最近的當前值。
? static contextType = ThemeContext;
? render() {
? ? return <Button theme={this.context} />;
? }
}
指定某個Context的寫法
...
class ThemedButton extends React.Component {
?//使用Consumer,獲取某個Context的值
? render() {
? ??? ?return (
? ? ? <ThemeContext.Consumer>
? ? ? ?? ?{theme => <Button {...props} theme={theme} />}
? ? ? </ThemeContext.Consumer>
? ? )
? }
}
我們同樣希望能夠在子組件中對Context的值做改變,這時候就需要用到回調函數
//創建一個Context?
//light就是傳入的默認值 最好傳入默認值因為第一次渲染的時候沒有默認值可能會導致錯誤
const ThemeContext = React.createContext({theme: 'light',toggle: ()=> {}});
class App extends React.Component {
? constructor(props) {
? ? super(props);
? ? this.toggle = () => { //設定toggle方法,會作為context參數傳遞
? ? ? this.setState(state => ({
? ? ? ? theme:
? ? ? ? ? state.theme === themes.dark
? ? ? ? ? ? ? themes.light
? ? ? ? ? ? : themes.dark,
? ? ? }));
? ? };
? ? this.state = {
? ? ? theme: themes.light,
? ? ? toggle: this.toggle,
? ? };
? }
? render() {
? ? return (
? ? ? ?
? ? ? <ThemeContext.Provider value={this.state}>
? ? ? ? <Toolbar />
? ? ? </ThemeContext.Provider>
? ? );
}
//中間組件
function Toolbar(props) {
? return (
? ? <div>
? ? ? <ThemedButton />
? ? </div>
? );
}
class ThemedButton extends React.Component {
?//使用Consumer,獲取某個Context的值
? render() {
? ??? ?return (
? ? ? <ThemeContext.Consumer>
? ? ? ?? ?{theme => <Button {...props} theme={theme} />}
? ? ? </ThemeContext.Consumer>
? ? )
? }
}
Context的值每次更新的時候,所有作為 Provider(提供者) 后代的 Consumer(使用者) 組件 都將重新渲染。
總的來說Context就是用來傳遞父子組件之前的狀態的api,防止多層組件傳遞狀態的問題,但是因為我們現在都有全局狀態管理的插件所以一般用不到, 但是其實在我們寫通用組件的時候可能我們不希望污染Redux的狀態樹,或者讓組件依賴于其他狀態插件,就可以用到這個功能?
使用context向后代組件傳參
當我們組件內嵌套多層后代組件的時候用props傳參就顯得繁瑣,且不美觀,這時候我們可以用context向后代組件直接傳參:
- 調用React.createContext()創建兩個組件(Provider、Consumer)分別用來提供數據和接收數據
- 使用Provider組件作為提供數據的父節點
- 給Provider組件設置value屬性,需要傳遞到后代組件中的數據作為value的值
- 調用Consumer組件接收數據(該組件內部是一個回調函數,形參就是從Provider組件傳過來的參數)
代碼示例:
class Parent extends React.Component {
state={
num:555,
color:'green'
}
render() {
return (
<Provider value={this.state.color}>
<div className="first">
<div>第1層</div>
<Css />
<div>第1層</div>
</div>
</Provider>
)
}
}
class Css extends React.Component {
render() {
return (
<div className="second">
<div>第2層</div>
<Js />
<div>第2層</div>
</div>
)
}
}
class Js extends React.Component {
render() {
return (
<div className="third">
<div>第3層</div>
<Html />
<div>第3層</div>
</div>
)
}
}
class Html extends React.Component {
render() {
return (
<div className="fourth">
<div>第4層</div>
<Consumer>{data=> <span>呼倫貝爾的顏色是{data}</span>}</Consumer>
<div>第4層</div>
</div>
)
}
}
原文鏈接:https://blog.csdn.net/deng1456694385/article/details/98601308
相關推薦
- 2022-01-08 vscode推送代碼失敗,報錯You are not allowed to push code to
- 2022-09-26 你了解Redis事務嗎_Redis
- 2022-04-11 C#創建控制Windows服務_C#教程
- 2023-11-11 tensorflow分布式報錯:tensorflow.python.framework.errors
- 2022-09-24 C#中參數的傳遞方式詳解_C#教程
- 2022-12-28 C++?Boost?Archive超詳細講解_C 語言
- 2022-12-04 Jetpack?Compose慣性衰減動畫AnimateDecay詳解_Android
- 2022-11-11 Android利用Canvas類繪制圖形_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支