最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React?Context詳解使用方法

 更新時(shí)間:2022年12月03日 11:36:10   作者:碼農(nóng)小菲  
Context提供了一個(gè)無需為每層組件手動(dòng)添加props,就能在組件樹間進(jìn)行數(shù)據(jù)傳遞的方法。在一個(gè)典型的?React?應(yīng)用中,數(shù)據(jù)是通過props屬性自上而下(由父及子)進(jìn)行傳遞的,但這種做法對于某些類型的屬性而言是極其繁瑣的

一、概述

  • Context 提供了一個(gè)無需為每層組件手動(dòng)添加 props,就能在組件樹間進(jìn)行數(shù)據(jù)傳遞的方法。
  • 如果多個(gè)組件中都需要這個(gè)值 或者 獲取值和使用值的層級(jí)相隔很遠(yuǎn),就可以使用Context(上下文)來共享數(shù)據(jù)。
  • 如:地區(qū)偏好,UI 主題、當(dāng)前認(rèn)證的用戶、語言等
  • 謹(jǐn)慎使用,這會(huì)使組件的復(fù)用性變差

二、API

React.createContext

const MyContext = React.createContext(defaultValue)
  • 創(chuàng)建一個(gè) Context 對象
  • 提供一個(gè)默認(rèn)值,只有當(dāng)組件所處的樹中沒有匹配到 Provider 時(shí),其 defaultValue 參數(shù)才會(huì)生效

Context.Provider

  const MyContext = React.createContext(defaultValue)
  <MyContext.Provider value={xxx}> ... </MyContext.Provider>
  • 每個(gè) Context 對象都會(huì)返回一個(gè) Provider React 組件
  • Provider 接收一個(gè) value 屬性,傳遞給消費(fèi)組件。一個(gè) Provider 可以對應(yīng)多個(gè)消費(fèi)組件。多個(gè) Provider 也可以嵌套使用,里層的會(huì)覆蓋外層的數(shù)據(jù)
  • value 值發(fā)生變化時(shí),它內(nèi)部的所有消費(fèi)組件都會(huì)重新渲染

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 屬性會(huì)被重賦值為一個(gè)由 React.createContext() 創(chuàng)建的 Context 對象
  • 可以在任意生命周期中訪問

Context.Consumer

<MyContext.Consumer>
   {value => /* 基于 context 值進(jìn)行渲染*/}
</MyContext.Consumer>
  • 消費(fèi)組件(子組件)中使用value
  • 函數(shù)作為子元素;這個(gè)函數(shù)接收當(dāng)前的 context 值,返回一個(gè) React 節(jié)點(diǎn)。傳遞給函數(shù)的 value 值等同于往上組件樹離這個(gè) 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支持獲取多個(gè)Context上的值

需要多個(gè)上下文的值時(shí)可以使用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 ThemedButton

3.useContext使用(函數(shù)式組件中使用)

react原生Hook ,讓函數(shù)式組件也可以使用Context,而且支持多個(gè)不同類型的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)建以太坊錢包實(shí)現(xiàn)轉(zhuǎn)賬等功能

    使用React Native創(chuàng)建以太坊錢包實(shí)現(xiàn)轉(zhuǎn)賬等功能

    這篇文章主要介紹了使用React Native創(chuàng)建以太坊錢包,實(shí)現(xiàn)轉(zhuǎn)賬等功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • React?redux?原理及使用詳解

    React?redux?原理及使用詳解

    這篇文章主要為大家介紹了React?redux?原理及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 詳解react-redux插件入門

    詳解react-redux插件入門

    這篇文章主要介紹了詳解react-redux插件入門,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • React Native設(shè)備信息查看調(diào)試詳解

    React Native設(shè)備信息查看調(diào)試詳解

    這篇文章主要為大家介紹了React Native設(shè)備信息查看調(diào)試詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 詳解React組件卸載怎么中止遞歸方法

    詳解React組件卸載怎么中止遞歸方法

    最近在處理項(xiàng)目代碼的時(shí)候,出現(xiàn)了一個(gè)bug,組件中的方法在組件卸載后仍然在執(zhí)行,代碼片段發(fā)給我看,但是變量的用意我也不懂,只看到有方法調(diào)用自身方法,這不就是遞歸嘛,所以本文詳細(xì)給大家介紹了React組件卸載怎么中止遞歸方法,需要的朋友可以參考下
    2024-01-01
  • React獲取Java后臺(tái)文件流并下載Excel文件流程解析

    React獲取Java后臺(tái)文件流并下載Excel文件流程解析

    這篇文章主要介紹了React獲取Java后臺(tái)文件流下載Excel文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • react中如何對自己的組件使用setFieldsValue

    react中如何對自己的組件使用setFieldsValue

    react中如何對自己的組件使用setFieldsValue問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • React實(shí)現(xiàn)骨架屏的示例代碼

    React實(shí)現(xiàn)骨架屏的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何通過React自動(dòng)化實(shí)現(xiàn)骨架屏,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • React中的useState和setState的執(zhí)行機(jī)制詳解

    React中的useState和setState的執(zhí)行機(jī)制詳解

    這篇文章主要介紹了React中的useState和setState的執(zhí)行機(jī)制,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • react之組件通信詳解

    react之組件通信詳解

    本篇文章主要介紹了React組件通信詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-10-10

最新評論

工布江达县| 陈巴尔虎旗| 弥渡县| 房产| 房山区| 天津市| 遂宁市| 长葛市| 临桂县| 巴林右旗| 拜泉县| 紫阳县| 偃师市| 巨鹿县| 章丘市| 桓仁| 仁化县| 从江县| 阿图什市| 宜都市| 万宁市| 汉沽区| 靖边县| 麦盖提县| 澄城县| 沭阳县| 京山县| 定日县| 金寨县| 潍坊市| 丹巴县| 中山市| 犍为县| 司法| 江孜县| 化德县| 宜城市| 大方县| 招远市| 盱眙县| 随州市|