React?Context詳解使用方法
一、概述
- Context 提供了一個無需為每層組件手動添加 props,就能在組件樹間進(jìn)行數(shù)據(jù)傳遞的方法。
- 如果多個組件中都需要這個值 或者 獲取值和使用值的層級相隔很遠(yuǎn),就可以使用Context(上下文)來共享數(shù)據(jù)。
- 如:地區(qū)偏好,UI 主題、當(dāng)前認(rèn)證的用戶、語言等
- 謹(jǐn)慎使用,這會使組件的復(fù)用性變差
二、API
React.createContext
const MyContext = React.createContext(defaultValue)
- 創(chuàng)建一個 Context 對象
- 提供一個默認(rèn)值,只有當(dāng)組件所處的樹中沒有匹配到 Provider 時,其 defaultValue 參數(shù)才會生效
Context.Provider
const MyContext = React.createContext(defaultValue)
<MyContext.Provider value={xxx}> ... </MyContext.Provider>
- 每個 Context 對象都會返回一個 Provider React 組件
- Provider 接收一個 value 屬性,傳遞給消費組件。一個 Provider 可以對應(yīng)多個消費組件。多個 Provider 也可以嵌套使用,里層的會覆蓋外層的數(shù)據(jù)
- value 值發(fā)生變化時,它內(nèi)部的所有消費組件都會重新渲染
Class.contextType
class MyClass extends React.Component {
componentDidMount() {
let value = this.context;
/* 在組件掛載完成后,使用 MyContext 組件的值來執(zhí)行一些有副作用的操作 */
}
componentDidUpdate() {
let value = this.context;
/* ... */
}
componentWillUnmount() {
let value = this.context;
/* ... */
}
render() {
let value = this.context;
/* 基于 MyContext 組件的值進(jìn)行渲染 */
}
}
MyClass.contextType = MyContext;
- 掛載在 class 上的 contextType 屬性會被重賦值為一個由 React.createContext() 創(chuàng)建的 Context 對象
- 可以在任意生命周期中訪問
Context.Consumer
<MyContext.Consumer>
{value => /* 基于 context 值進(jìn)行渲染*/}
</MyContext.Consumer>
- 消費組件(子組件)中使用value
- 函數(shù)作為子元素;這個函數(shù)接收當(dāng)前的 context 值,返回一個 React 節(jié)點。傳遞給函數(shù)的 value 值等同于往上組件樹離這個 context 最近的 Provider 提供的 value 值。如果沒有對應(yīng)的 Provider,value 參數(shù)等同于傳遞給 createContext() 的 defaultValue。
Context.displayName
const MyContext = React.createContext() MyContext.displayName = 'MyDisplayName' <MyContext.Provider> //在 DevTools 中顯示的標(biāo)簽:MyDisplayName.Provider <MyContext.Consumer>//在 DevTools 中顯示的標(biāo)簽:MyDisplayName.Consumer //如果沒有 MyContext.displayName = 'MyDisplayName' ,則顯示Context.Provider、Context.Consumer
在 DevTools 中需要顯示的內(nèi)容
三、使用
1.自定義Context (類組件中使用)
//ThemeContext.js
import React from 'react'
export const ThemeContext = React.createContext('light')
//themed-button.js
//在需要的位置使用 Class.contextType
import React, { Component } from 'react';
import ThemeContext from "./context/ThemeContext.js";
class ThemedButton extends Component {
static contextType = ThemeContext;
render() {
return <button>{this.context}</button>;
}
}
export default ThemedButton
//app.js
//上下文包裹組件 Context.Provider
import ThemeContext from './context/ThemeContext.js';
import ThemedButton from './ThemedButton.js';
import './App.css';
function App() {
return (
<ThemeContext.Provider value='dark'> //dark將默認(rèn)值light覆蓋
<div className="App">
<header className="App-header">
<ThemedButton />
</header>
</div>
</ThemeContext.Provider>
);
}
export default App;2.使用Consumer支持獲取多個Context上的值
需要多個上下文的值時可以使用Consumer
//ThemeContext.js
import React from 'react'
const ThemeContext = React.createContext('light')
ThemeContext.displayName = 'ThemeContext'
export default ThemeContext
//UserContext.js
import React from 'react'
const UserContext = React.createContext('guest')
UserContext.displayName = 'UserContext'
export default UserContext
//app.js
//使用Provider賦值:
import React, { Component } from 'react';
import ThemeContext from './context/ThemeContext.js';
import UserContext from './context/UserContext.js';
import ThemedButton from './ThemedButton.js';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
}
render() {
return (
<ThemeContext.Provider value={'dark'}>
<div className="App">
<UserContext.Provider value={'user'}>
<header className="App-header">
<ThemedButton />
</header>
</UserContext.Provider>
</div>
</ThemeContext.Provider>
);
}
}
export default App
//themed-button.js
import React, { Component } from 'react';
import ThemeContext from "./context/ThemeContext.js";
import UserContext from "./context/UserContext.js";
class ThemedButton extends Component {
render() {
return (
<>
<ThemeContext.Consumer>
{theme => <div>{theme}</div>}
</ThemeContext.Consumer>
<UserContext.Consumer>
{user => <div>{user}</div>}
</UserContext.Consumer>
</>
);
}
}
export default ThemedButton3.useContext使用(函數(shù)式組件中使用)
react原生Hook ,讓函數(shù)式組件也可以使用Context,而且支持多個不同類型的context
//ThemeContext.js
import React from 'react'
const ThemeContext = React.createContext('light')
ThemeContext.displayName = 'ThemeContext'
export default ThemeContext
//UserContext.js
import React from 'react'
const UserContext = React.createContext('guest')
UserContext.displayName = 'UserContext'
export default UserContext
//app.js
//使用Provider賦值:
import React, { Component } from 'react';
import ThemeContext from './context/ThemeContext.js';
import UserContext from './context/UserContext.js';
import ThemedButton from './ThemedButton.js';
import './App.css';
const App = () => {
render() {
return (
<ThemeContext.Provider value={'dark'}>
<div className="App">
<UserContext.Provider value={'user'}>
<header className="App-header">
<ThemedButton />
</header>
</UserContext.Provider>
</div>
</ThemeContext.Provider>
);
}
}
export default App
//themed-button.js
import { useContext } from 'react'
import ThemeContext from './context/ThemeContext'
import UserContext from './context/UserContext'
const ThemedButton = () => {
const theme = useContext(ThemeContext)
const user = useContext(UserContext)
return (
<>
<div>{theme}</div>
<div>{user}</div>
</>
)
}
export default ThemedButton到此這篇關(guān)于React Context詳解使用方法的文章就介紹到這了,更多相關(guān)React Context內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用React Native創(chuàng)建以太坊錢包實現(xiàn)轉(zhuǎn)賬等功能
這篇文章主要介紹了使用React Native創(chuàng)建以太坊錢包,實現(xiàn)轉(zhuǎn)賬等功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
React Native設(shè)備信息查看調(diào)試詳解
這篇文章主要為大家介紹了React Native設(shè)備信息查看調(diào)試詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
React獲取Java后臺文件流并下載Excel文件流程解析
這篇文章主要介紹了React獲取Java后臺文件流下載Excel文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
react中如何對自己的組件使用setFieldsValue
react中如何對自己的組件使用setFieldsValue問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
React中的useState和setState的執(zhí)行機(jī)制詳解
這篇文章主要介紹了React中的useState和setState的執(zhí)行機(jī)制,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03

