跳至主要内容

2 篇文章 含有標籤「React.js」

檢視所有標籤

React Context API 入門教學 | 學習筆記

· 閱讀時間約 5 分鐘
kdchang

React Context API 是 React 提供的一種方式,讓我們能夠在組件樹中傳遞資料,而不需要一層層地使用 props。Context API 可以解決多層嵌套組件的傳遞問題,讓我們在深層組件中輕鬆訪問到全局狀態。本文將介紹如何使用 React Context API,並提供一個簡單的範例來展示其實際應用。

什麼是 React Context API

React Context API 是 React 的一個內建功能,它可以讓我們在組件樹中共享資料,避免多層嵌套的 props 傳遞。Context 主要由三個部分組成:

  1. React.createContext():創建一個 Context 物件。
  2. Provider:這是 Context API 中的一個組件,它用來包裹整個應用,並提供一個全局的資料源。
  3. Consumer:這是用來訪問 Context 資料的組件,它能夠獲取 Provider 中傳遞的資料。

使用 Context 的目的,是為了避免將相同的資料層層傳遞到每個組件,這樣可以讓應用的資料流變得更加簡潔。

使用 Context API 的步驟

步驟 1: 創建 Context

首先,我們需要使用 React.createContext() 來創建一個 Context 物件。這個物件會返回一個 ProviderConsumer 組件,讓我們在應用中使用。

import React from 'react';

// 創建 Context
const MyContext = React.createContext();

步驟 2: 使用 Provider 來傳遞資料

Context 的 Provider 是用來包裹應用的,它會接收一個 value 屬性,這個屬性就是要共享給整個組件樹的資料。

const App = () => {
const [user, setUser] = React.useState({ name: 'John', age: 30 });

return (
<MyContext.Provider value={user}>
<UserProfile />
</MyContext.Provider>
);
};

在這個範例中,我們將一個 user 物件傳遞給 MyContext.Providervalue 屬性,這樣整個組件樹中的所有子組件都能夠訪問到這個 user 資料。

步驟 3: 使用 Consumer 來接收資料

在需要使用資料的地方,我們可以使用 MyContext.Consumer 來獲取資料。Consumerchildren 是一個函數,它會接收一個 value 參數,這個參數就是在 Provider 中傳遞的資料。

const UserProfile = () => {
return (
<MyContext.Consumer>
{(user) => (
<div>
<h1>{user.name}</h1>
<p>Age: {user.age}</p>
</div>
)}
</MyContext.Consumer>
);
};

在這個範例中,UserProfile 組件通過 Consumer 來訪問 MyContext 中的 user 資料,並渲染顯示用戶的名字和年齡。

步驟 4: 使用 useContext Hook (React 16.8 及以上)

React 16.8 引入了 useContext Hook,這樣我們可以更方便地在函數組件中使用 Context,而不需要使用 Consumer。這樣的寫法更加簡潔,並且避免了過多的嵌套。

import React, { useContext } from 'react';

const UserProfile = () => {
const user = useContext(MyContext);

return (
<div>
<h1>{user.name}</h1>
<p>Age: {user.age}</p>
</div>
);
};

使用 useContext 可以直接從 Context 中獲取資料,而不需要使用 Consumer。這使得代碼更簡潔,並提高了可讀性。

實際範例

下面是一個完整的範例,展示了如何使用 React Context API 來管理應用中的全局狀態。這個範例將包括一個用戶資料的管理,並能夠在多個組件中共享這些資料。

import React, { useState, useContext } from 'react';

// 創建 Context
const MyContext = React.createContext();

const App = () => {
const [user, setUser] = useState({ name: 'John', age: 30 });

return (
<MyContext.Provider value={user}>
<div>
<UserProfile />
<AgeUpdater />
</div>
</MyContext.Provider>
);
};

const UserProfile = () => {
const user = useContext(MyContext);

return (
<div>
<h1>{user.name}</h1>
<p>Age: {user.age}</p>
</div>
);
};

const AgeUpdater = () => {
const user = useContext(MyContext);
const setUser = useState()[1];

const updateAge = () => {
setUser({ ...user, age: user.age + 1 });
};

return (
<div>
<button onClick={updateAge}>Increase Age</button>
</div>
);
};

export default App;

範例解析

  1. App 組件:在 App 組件中,我們使用 useState 定義了一個 user 資料,並通過 MyContext.Provider 將資料提供給下層組件。
  2. UserProfile 組件UserProfile 使用 useContext 來讀取 MyContext 中的資料,並顯示用戶的名字和年齡。
  3. AgeUpdater 組件:這個組件同樣使用 useContext 來讀取和更新 user 資料。我們在這裡定義了一個按鈕,當按下時,會更新 user 的年齡。

Context API 的優缺點

優點:

  1. 簡化資料傳遞:當我們需要在多層嵌套的組件中共享資料時,使用 Context 可以避免繁瑣的 props 傳遞。
  2. 可擴展性:Context 非常適合用於應用中的全局狀態管理,像是用戶認證、語言設置、主題樣式等。

缺點:

  1. 重新渲染問題:當 Provider 中的資料變更時,所有使用該 Context 的組件都會重新渲染。對於大型應用來說,這可能會影響性能。
  2. 狀態過度共享:Context 主要用於共享全局資料,如果將太多不相關的資料放入同一個 Context,可能會使代碼變得難以維護。

總結

React Context API 是一個強大的工具,可以幫助我們管理應用中的全局狀態。在適當的情況下使用 Context 可以大大簡化代碼,避免深層嵌套的 props 傳遞。但也需要謹慎使用,避免過多不必要的資料共享,從而影響性能和可維護性。在開發中,我們可以根據具體需求來選擇是否使用 Context API,並搭配其他狀態管理工具(如 Redux 或 Zustand)來管理更複雜的應用狀態。

參考文件

  1. 用 React Context API 實作跨組件傳值的功能

React useMemo 與 useCallback 差異介紹與入門教學 | 學習筆記

· 閱讀時間約 4 分鐘
kdchang

在使用 React hooks 進行開發時,useMemouseCallback 是兩個常被提及的性能優化工具。它們都屬於 記憶化(memoization) 技術,用來避免不必要的重算與重渲染。然而,很多初學者在理解這兩者的用途與差異時常感到困惑。這篇文章將從概念出發,並搭配實際範例,幫助你掌握 useMemouseCallback 的核心用途與實作方式。


一、共通點:記憶化

React 在每次組件渲染時,預設會重新執行所有函式與表達式。當某些值(如計算結果、函式)在依賴未改變的情況下不需要重新產生,我們可以利用記憶化來優化效能。

這就是 useMemouseCallback 的主要功能:根據依賴陣列(dependency array)決定是否重建值或函式


二、差異概念總覽

Hook主要用途回傳內容使用時機
useMemo記憶「計算結果」任意值計算過程昂貴,避免重複運算
useCallback記憶「函式定義」函式傳遞函式給子元件,避免不必要的 re-render

換句話說:

  • useMemo(fn, deps)const value = memoized(fn)
  • useCallback(fn, deps)const callback = memoized(() => fn)

三、useMemo 範例

假設我們有一個需要進行繁重計算的函式,例如統計某個資料集合中的數值:

import React, { useState, useMemo } from 'react';

function slowFunction(num) {
console.log('Running slow function');
let result = 0;
for (let i = 0; i < 1e7; i++) {
result += num;
}
return result;
}

function ExpensiveComponent() {
const [count, setCount] = useState(0);
const [input, setInput] = useState(1);

const computedValue = useMemo(() => {
return slowFunction(input);
}, [input]);

return (
<div>
<h2>Expensive Calculation</h2>
<p>計算結果:{computedValue}</p>
<input
type="number"
value={input}
onChange={e => setInput(Number(e.target.value))}
/>
<button onClick={() => setCount(c => c + 1)}>重新渲染 ({count})</button>
</div>
);
}

在這個例子中,若不使用 useMemo,只要任何 state 改變(例如點擊按鈕改變 count),整個組件都會重新執行 slowFunction,導致效能問題。透過 useMemo,只有 input 改變時才會重新計算,其他情況會重複使用上次的計算結果。


四、useCallback 範例

有時候我們會將函式作為 props 傳遞給子元件。如果每次重新 render 都產生新的函式實例,會導致子元件誤以為 props 改變,而重新渲染。這時就可以用 useCallback

import React, { useState, useCallback, memo } from 'react';

const ChildButton = memo(({ onClick }) => {
console.log('ChildButton rendered');
return <button onClick={onClick}>點我</button>;
});

function ParentComponent() {
const [count, setCount] = useState(0);
const [other, setOther] = useState(0);

const handleClick = useCallback(() => {
setCount(c => c + 1);
}, []);

return (
<div>
<h2>useCallback Demo</h2>
<p>Count: {count}</p>
<ChildButton onClick={handleClick} />
<button onClick={() => setOther(o => o + 1)}>改變其他狀態</button>
</div>
);
}

在這個範例中,ChildButton 是經過 memo 包裹的元件,只有在 props.onClick 改變時才會重新渲染。使用 useCallback 確保 handleClick 函式在 [](無依賴)下只會創建一次,即使 other 改變,ChildButton 也不會重新渲染。


五、常見錯誤與注意事項

  1. 過度使用會反效果
    useMemouseCallback 本身也有記憶成本,不建議過度使用。只有在你確定函式或運算昂貴,或造成子元件重 render 才需要用。

  2. 依賴陣列要正確
    記得將函式中引用的變數正確加入依賴陣列中,否則會造成記憶結果與預期不符。

  3. 搭配 React.memo 效果更明顯
    useCallback 通常與 memoPureComponent 搭配,否則即使函式地址一樣,也無法避免重 render。


六、總結

當你在開發中遇到效能瓶頸或元件不必要地重複渲染時,才是使用 useMemouseCallback 的好時機。舉例來說:

  • 在表格過濾、排序等涉及大量資料處理的畫面中,可以用 useMemo 優化計算。
  • 在表單中將函式傳遞給 input 元件時,使用 useCallback 可避免整個表單重 render。

記住一個原則:不要為了使用 hook 而使用,而是根據實際效能需求進行優化。如果你的應用很小或尚未遇到效能問題,先專注於撰寫可讀性高、邏輯清楚的程式碼,這才是最重要的。

參考文件

  1. 如何優化 React 元件的渲染效能,並避免渲染陷阱