react中關(guān)于Context/Provider/Consumer傳參的使用
Context/Provider/Consumer傳參使用
react context這個api很少用到,所以一直不太清楚如何使用,最近在看antd的項目源碼時,發(fā)現(xiàn)在組件中有類似Template.Comsumer的寫法,一時沒反應(yīng)過來,本著碰到不懂得都要追根究底的原則,下面好好學(xué)習(xí)一下,Context這個api的使用
Context
作用
上下文(Context) 提供了一種通過組件樹傳遞數(shù)據(jù)的方法,無需在每個級別手動傳遞 props 屬性。
但是我們現(xiàn)在通用的傳遞數(shù)據(jù)的做法是使用redux,mobx,應(yīng)為這些數(shù)據(jù)管理插件,能更好的對數(shù)據(jù)做管理已經(jīng)更新 ,而使用Context只能將部分?jǐn)?shù)據(jù)傳入,且使用起來比較麻煩,容易造成混亂.
所以一般情況下我們不要使用Context,雖然不太用到但是還是需要學(xué)習(xí)的,指不定哪天就轉(zhuǎn)正了
如何使用
這里直接就用官方的例子來解釋,自己懶得寫例子了 = ) !
//創(chuàng)建一個Context?
//light就是傳入的默認(rèn)值 最好傳入默認(rèn)值因為第一次渲染的時候沒有默認(rèn)值可能會導(dǎo)致錯誤
const ThemeContext = React.createContext('light');
class App extends React.Component {
? render() {
? ? return (
? ? ? ? //使用Provider設(shè)置dark值
? ? ? <ThemeContext.Provider value="dark">
? ? ? ? <Toolbar />
? ? ? </ThemeContext.Provider>
? ? );
}
//中間組件
function Toolbar(props) {
? return (
? ? <div>
? ? ? <ThemedButton />
? ? </div>
? );
}
class ThemedButton extends React.Component {
//注冊context自動獲取當(dāng)前組件最近的Provider,獲取到context的值 ,這種寫法是不直接指定context的寫法
//可以為類上的 contextType 屬性分配由 React.createContext() 創(chuàng)建的 Context 對象。 這允許您使用this.context 使用該 Context 類型 的最近的當(dāng)前值。
? 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的值做改變,這時候就需要用到回調(diào)函數(shù)
//創(chuàng)建一個Context?
//light就是傳入的默認(rèn)值 最好傳入默認(rèn)值因為第一次渲染的時候沒有默認(rèn)值可能會導(dǎo)致錯誤
const ThemeContext = React.createContext({theme: 'light',toggle: ()=> {}});
class App extends React.Component {
? constructor(props) {
? ? super(props);
? ? this.toggle = () => { //設(shè)定toggle方法,會作為context參數(shù)傳遞
? ? ? 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就是用來傳遞父子組件之前的狀態(tài)的api,防止多層組件傳遞狀態(tài)的問題,但是因為我們現(xiàn)在都有全局狀態(tài)管理的插件所以一般用不到, 但是其實在我們寫通用組件的時候可能我們不希望污染Redux的狀態(tài)樹,或者讓組件依賴于其他狀態(tài)插件,就可以用到這個功能
使用context向后代組件傳參
當(dāng)我們組件內(nèi)嵌套多層后代組件的時候用props傳參就顯得繁瑣,且不美觀,這時候我們可以用context向后代組件直接傳參:
- 調(diào)用React.createContext()創(chuàng)建兩個組件(Provider、Consumer)分別用來提供數(shù)據(jù)和接收數(shù)據(jù)
- 使用Provider組件作為提供數(shù)據(jù)的父節(jié)點
- 給Provider組件設(shè)置value屬性,需要傳遞到后代組件中的數(shù)據(jù)作為value的值
- 調(diào)用Consumer組件接收數(shù)據(jù)(該組件內(nèi)部是一個回調(diào)函數(shù),形參就是從Provider組件傳過來的參數(shù))
代碼示例:
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>
)
}
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
react echarts tooltip 區(qū)域新加輸入框編輯保存數(shù)據(jù)功能
這篇文章主要介紹了react echarts tooltip 區(qū)域新加輸入框編輯保存數(shù)據(jù)功能,大概思路是用一個div包裹echarts, 然后在echarts的同級新建一個div用來用來模擬真實tooltip,通過鼠標(biāo)移入移出事件控制真實tooltip的顯示與隱藏,需要的朋友可以參考下2023-05-05
React 使用browserHistory項目訪問404問題解決
這篇文章主要介紹了React 使用browserHistory項目訪問404問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
react?umi?刷新或關(guān)閉瀏覽器時清除localStorage方式
這篇文章主要介紹了react?umi?刷新或關(guān)閉瀏覽器時清除localStorage方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
一起來學(xué)習(xí)React元素的創(chuàng)建和渲染
這篇文章主要為大家詳細(xì)介紹了React元素的創(chuàng)建和渲染,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
解決react中useState狀態(tài)異步更新的問題
本文主要介紹了react中useState狀態(tài)異步更新的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

