React Context詳解使用過程
1.Context是干嘛的
一種React組件間通信方式, 常用于【祖組件】與【后代組件】間通信
2.可以倒是可以實(shí)現(xiàn)的做法-props逐級傳遞
import React, { Component } from "react";
import "./index.css";
export default class A extends Component {
state = { username: "tom", age: 18 };
render() {
const { username, age } = this.state;
return (
<div className="parent">
<h3>我是A組件</h3>
<h4>我的用戶名是:{username}</h4>
<B username={username} age={age}></B>
</div>
);
}
}
class B extends Component {
render() {
console.log(this.props);
const { username, age } = this.props;
return (
<div className="child">
<h3>我是B組件</h3>
<C username={username} age={age} />
</div>
);
}
}
function C(props) {
const { username, age } = props;
return (
<div className="grand">
<h3>我是C組件</h3>
<h4>
我從A組件接收到的用戶名:{username},年齡是{age}
</h4>
</div>
);
}
這里C組件用了下函數(shù)式組件的寫法

但是這種寫法有很多的不太好的地方,如果層級再深一點(diǎn)呢,傳的值再多一點(diǎn)呢??隙〞泻芏嗟闹貜?fù)代碼出現(xiàn),而且我只想要祖組件和孫組件通信,這樣寫的話其實(shí)是每個組件都在通信了的
3.Context
Context的就可以挺好的適用于這種場景
創(chuàng)建Context容器對象:
const XxxContext = React.createContext()
2) 渲染子組時,外面包裹xxxContext.Provider, 通過value屬性給后代組件傳遞數(shù)據(jù):
<xxxContext.Provider value={數(shù)據(jù)}>
子組件
</xxxContext.Provider>
3) 后代組件讀取數(shù)據(jù):
//第一種方式:僅適用于類組件
static contextType = xxxContext // 聲明接收context
this.context // 讀取context中的value數(shù)據(jù)
//第二種方式: 函數(shù)組件與類組件都可以
<xxxContext.Consumer>
{
value => ( // value就是context中的value數(shù)據(jù)
要顯示的內(nèi)容
)
}
</xxxContext.Consumer>
上代碼
import React, { Component } from "react";
import "./index.css";
//創(chuàng)建Context對象
const MyContext = React.createContext();
const { Provider, Consumer } = MyContext;
export default class A extends Component {
state = { username: "tom", age: 18 };
render() {
const { username, age } = this.state;
return (
<div className="parent">
<h3>我是A組件</h3>
<h4>我的用戶名是:{username}</h4>
//如果傳的只有一個值例如username,應(yīng)該式value={username},后面就是直接獲取了,不需要再屬性名取值了
<Provider value={{ username, age }}>
<B />
</Provider>
</div>
);
}
}
class B extends Component {
render() {
return (
<div className="child">
<h3>我是B組件</h3>
<C/>
</div>
);
}
}
class C extends Component {
//聲明接收context
static contextType = MyContext
render() {
const {username,age} = this.context
return (
<div className="grand">
<h3>我是C組件</h3>
<h4>我從A組件接收到的用戶名:{username},年齡是{age}</h4>
</div>
)
}
}
// function C() {
// return (
// <div className="grand">
// <h3>我是C組件</h3>
// <h4>
// 我從A組件接收到的用戶名:
// {/* <Consumer>{(value) => `${value.username},年齡是${value.age}`}</Consumer> */}
// </h4>
// </div>
// );
// }
到此這篇關(guān)于React Context詳解使用過程的文章就介紹到這了,更多相關(guān)React Context內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react開發(fā)中如何使用require.ensure加載es6風(fēng)格的組件
本篇文章主要介紹了react開發(fā)中如何使用require.ensure加載es6風(fēng)格的組件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
React Native時間轉(zhuǎn)換格式工具類分享
這篇文章主要為大家分享了React Native時間轉(zhuǎn)換格式工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
React報錯Function?components?cannot?have?string?refs
這篇文章主要為大家介紹了React報錯Function?components?cannot?have?string?refs解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
解決react-connect中使用forwardRef遇到的問題
這篇文章主要介紹了解決react-connect中使用forwardRef遇到的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
React系列useSyncExternalStore學(xué)習(xí)詳解
這篇文章主要為大家介紹了React系列useSyncExternalStore的學(xué)習(xí)及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
基于React實(shí)現(xiàn)一個todo打勾效果
這篇文章主要為大家詳細(xì)介紹了如何基于React實(shí)現(xiàn)一個todo打勾效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

