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

React中的Context應(yīng)用場景分析

 更新時間:2021年06月11日 10:07:23   作者:moakap  
這篇文章主要介紹了React中的Context應(yīng)用場景分析,Context 提供了一種在組件之間共享數(shù)據(jù)的方式,而不必顯式地通過組件樹的逐層傳遞 props,通過實例代碼給大家介紹使用步驟,感興趣的朋友跟隨小編一起看看吧

Context定義和目的

Context 提供了一種在組件之間共享數(shù)據(jù)的方式,而不必顯式地通過組件樹的逐層傳遞 props。

應(yīng)用場景 哪些數(shù)據(jù)會需要共享?

Context 設(shè)計目的是為了共享那些對于一個組件樹而言是**“全局”的數(shù)據(jù)**,例如當前認證的用戶、主題或首選語言。

使用步驟

1. 創(chuàng)建并初始化Context

const MyContext = createContex(defaultValue);

創(chuàng)建一個 Context 對象。當 React 渲染一個訂閱了這個 Context 對象的組件,這個組件會從組件樹中離自身最近的那個匹配的 Provider 中讀取到當前的 context 值。

2. 訂閱Context

<MyContext.Provider value={/* 某個值 */}>

Provider 接收一個 value 屬性,傳遞給消費組件。一個 Provider 可以和多個消費組件有對應(yīng)關(guān)系。多個 Provider 也可以嵌套使用,里層的會覆蓋外層的數(shù)據(jù)。

這里有兩個相關(guān)的概念

  • Provider - Context提供者,或Context的訂閱者??梢岳斫鉃橥ㄟ^Provider為其內(nèi)部組件訂閱了Context值的變動,一旦Context值有變化,就會觸發(fā)內(nèi)部組件重新渲染。
  • Comsumer - Context消費者(消費組件),或者叫Context使用者。即在Provider內(nèi)部使用useContext()來使用或消費Context的組件。這些組件通過useContext()獲取、使用Context的最新值。

3. 使用Conext

3.1 React組件中使用

const value = useContext(MyContext);

在消費組件中引用Context。value會從組件樹中離自身最近的那個匹配的Provider中讀取到當前的Context值。

3.2 純函數(shù)式組件中使用

在純函數(shù)式的組件中,可以使用Consumer來引用context的值。如果沒有上層對應(yīng)的Provider,value等同于傳遞給createContext()defaultValue.

<MyContext.Consumer>
  {value => /* 基于 context 值進行渲染*/}
</MyContext.Consumer>

4. Context的更新

4.1 自上而下更新Context

自上而下更新指的是更新Provider的value值。當 Provider 的 value 值發(fā)生變化時,它內(nèi)部的所有消費組件內(nèi)通過useContext獲取到的值會自動更新,并觸發(fā)重新渲染。

//App.js

// ....

export default function App() {
    //...
    
    // 
    const {contextValue, setContextValue} = React.useState(initialValue);

    // function to update the context value
    function updateContext(newValue) {
        // ...

        // 更新contextValue, ConsumerComponent1, ConsumerComponent2, ConsumerComponent3, ConsumerComponent11都會觸發(fā)重新渲染。
        setContextValue(newValue)
    }

    ...
    return (
        <App>
            <MyContext.Provider value={contextValue}>
                <ConsumerComponent1>
                    <ConsumerComponent11>
    					// ....
                    </ComsumerComponent11>
                </ConsumerComponent1>

                <ConsumerComponent2 />
                <ConsumerComponent3 />
            </MyContext.Provider>
        </App>
    );
    
}

4.2 自下而上(從消費組件)更新Context

在某些情況下,需要在某個消費組件內(nèi)更新context,并且適配到整個程序。比如通過應(yīng)用程序的setting組件修改UI風格。 這時就需要通過回調(diào)將更新一層層傳遞到對應(yīng)的Provider,更新Provide對應(yīng)的value,從而觸發(fā)所有相關(guān)消費組件的更新。

// app.js

export default function App() {
    ...
    const {contextValue, setContextValue} = React.useState(initialValue);

    // function to update the context value
    function updateContext(newValue) {
        // ...

        // 更新contextValue, ConsumerComponent1, ConsumerComponent2, ConsumerComponent3, ConsumerComponent11都會觸發(fā)重新渲染。
        setContextValue(newValue)
    }

    ...
    return (
        <App>
            <MyContext.Provider value={contextValue}>
                <ConsumerComponent1>
                    <ConsumerComponent11 updateValue={updateContext}> // 通過回調(diào)形式的props, 在ConsumerComponent11中更新contextValue, 因為contextValue屬于最頂層的Provider的值,所以也會觸發(fā)ConsumerComponent1, ConsumerComponent2, ConsumerComponent3重新渲染。
                    </ComsumerComponent11>
                </ConsumerComponent1>

                <ConsumerComponent2 />
                <ConsumerComponent3 />
            </MyContext.Provider>
        </App>
    );
}

4.3 Provider嵌套

在一些情況下,可能會出現(xiàn)同一個Context的provider嵌套的情況,這時候可以理解為兩個Context。不同的是,

...
const {contextValue, setContextValue} = React.useState(initialValue);

// function to update the context value
function updateContext(newValue) {
    // ...
    
    // 更新contextValue, ConsumerComponent1, ConsumerComponent2, ConsumerComponent3, ConsumerComponent11都會觸發(fā)重新渲染。
    setContextValue(newValue)
}

...
return (
	<App>
        <MyContext.Provider value={contextValue}>
            <ConsumerComponent1>
                <ConsumerComponent11 />
            </ConsumerComponent1>

            <ConsumerComponent2>
                ...
                // 如果只希望更新ComsumerComponent21, ComsumerComponent22中的值
                
                const localContextValue = useContext(MyContext); // 從上一層Provider中獲取當前值

				const {tempContextValue, setTempContextValue} = React.useState(localContextValue);

				function updateTempContext(newValue) {
                    // 這里更新以后只會觸發(fā)ConsumerComponent21和ConsumerComponent22的重新渲染
                    setTempContextValue(newValue); 
                }

				// 這里新建Provider,在ConsumerComponent21和ConsumerComponent22之間共享數(shù)據(jù)。
                <MyContext.Provider value={tempValue}>
                    <ConsumerComponent21>
                    	// 在ConsumerComponent21中通過useContext(MyContext)訂閱
                    	// 獲取到的值為離自身最近的那個匹配的Provider中讀取到的Context值,即tempValue
                    </ConsumerComponent21>
                    <ConsumerComponent22>
                    </ConsumerComponent22>
				</MyContext.Provider value={contextValue}>
            </ConsumerComponent2>
            <ConsumerComponent3 />
        </MyContext.Provider>
    </App>
);

官方文檔

官方文檔請參考下邊的基礎(chǔ)和高級教程。

Hook API 索引 – React (reactjs.org)

Context – React (reactjs.org)

以上就是React中的Context應(yīng)用場景分析的詳細內(nèi)容,更多關(guān)于React中的Context的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 基于React實現(xiàn)下拉刷新效果

    基于React實現(xiàn)下拉刷新效果

    這篇文章主要介紹了如何基于react實現(xiàn)下拉刷新效果,在下拉的時候會進入loading狀態(tài),文中有詳細的代碼示例供大家參考,對大家的學(xué)習或工作有一定的幫助,需要的朋友可以參考下
    2024-03-03
  • React表單容器的通用解決方案

    React表單容器的通用解決方案

    本文主要介紹了React表單容器的通用解決方案,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • React組件與事件的創(chuàng)建使用教程

    React組件與事件的創(chuàng)建使用教程

    react事件綁定時。this并不會指向當前DOM元素。往往使用bind來改變this指向,今天通過本文給大家介紹React事件綁定的方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習吧
    2023-02-02
  • react如何添加less環(huán)境配置

    react如何添加less環(huán)境配置

    這篇文章主要介紹了react如何添加less環(huán)境配置,本文給大家分享遇到問題及解決方案,結(jié)合示例代碼圖文給大家介紹的非常詳細,需要的朋友參考下吧
    2022-05-05
  • React翻頁器的實現(xiàn)(包含前后端)

    React翻頁器的實現(xiàn)(包含前后端)

    本文主要介紹了React翻頁器的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 淺談React Event實現(xiàn)原理

    淺談React Event實現(xiàn)原理

    這篇文章主要介紹了淺談React Event實現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • React中classnames庫使用示例

    React中classnames庫使用示例

    這篇文章主要為大家介紹了React中classnames庫使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • React hook 'useState' is called conditionally報錯解決

    React hook 'useState' is calle

    這篇文章主要為大家介紹了React hook 'useState' is called conditionally報錯解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • react+ts實現(xiàn)簡單jira項目的最佳實踐記錄

    react+ts實現(xiàn)簡單jira項目的最佳實踐記錄

    這篇文章主要介紹了react+ts實現(xiàn)簡單jira項目,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • React tabIndex使非表單元素支持focus和blur事件

    React tabIndex使非表單元素支持focus和blur事件

    這篇文章主要為大家介紹了React使用tabIndex使非表單元素支持focus和blur事件實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論

巴东县| 顺义区| 南华县| 凤翔县| 司法| 牡丹江市| 马公市| 呼图壁县| 镶黄旗| 门头沟区| 郯城县| 连江县| 贵溪市| 永清县| 买车| 岳阳县| 肥东县| 湟源县| 郴州市| 双鸭山市| 襄樊市| 永昌县| 乐安县| 沧州市| 龙川县| 双峰县| 昆明市| 吉隆县| 赤峰市| 宁远县| 微山县| 凤冈县| 合江县| 兖州市| 安阳县| 福清市| 长海县| 黄大仙区| 枞阳县| 吉水县| 鄂托克旗|