React Context 是道具鑽探的解毒劑嗎? (Is React Context an antidote for prop drilling?)


問題描述

React Context 是道具鑽探的解毒劑嗎? (Is React Context an antidote for prop drilling?)

如果 React Context API 旨在用於傳遞全局變量,我們為什麼要使用它們來代替從父組件到子組件的傳遞道具(道具鑽探)?由於大多數傳遞的道具並不意味著在應用程序範圍內可用,即全局可用。


參考解法

方法 1:

The new react context api allows you to "scope" the values, you don't have to wrap your whole application with the context provider, you can only wrap the part of the component tree where you need particular props. It can be useful when your component tree is deeply nested and you'd need to pass certain props multiple levels.

方法 2:

The variables or values defined in context are available to any component that tries to destructure those values. However, if you have any setters that change those defined values, only the children that are passed to the Provider will get the updated values.

For instance, if we create a context myContext and define name and age, we have to then set up a provider that will provide it's children that information to consume.

const myContext = createContext({ 
  name: 'Bob',
  age: 35
});

Now, we can use a Provider to pass that data down to children.

function HelloWorld() { 
 const { Provider } = myContext;
 const [age, setAge] = useState(35)
 const [name, setName] = useState('Bob') 
 return (
   <Provider value={{ name, age }}>
     <Profile />
   </Provider>
 )
}

name and age are the values we want to expose to our children, in this case we just have one child Profile. Now we can access name and age in Profile by destructuring them from our context.

function Profile(){
 const { name, age } = useContext(myContext)

 return (
  <ul>
   <li>{name}</li>
   <li>{age}</li>
  </ul>
 )
}

But let's say somewhere else in our project we have component called Foo and we want to access name.

function Foo() {
 const { name } = useContext(myContext) // you will only receive the default values defined in context
 return <p>{name}</p>
}

This will return the default 'Bob' defined in myContext. You may think, what was the point of that?

If we update our HelloWorld component to actually update the name and age onMount, Foo will still show Bob.

function HelloWorld() {
  const { Provider } = myContext;
  const [age, setAge] = useState("");
  const [name, setName] = useState("");
  useEffect(() => {
    setAge(40);
    setName("Bill");
  }, []);
  return (
    <Provider value={{ name, age }}>
      <Profile />
    </Provider>
  );
}

function Profile() { 
 return ( 
  <ul>
    <li>{name}</li> // returns Bill
    <li>{age}</li>  // returns 40
  </ul>
 )
}

function Foo() { 
 return (
  <p>{name}</p> // returns Bob 
 )
}

This is great when you have isolated features or components that need to pass around data and setters instead of prop drilling. You can have a component consume multiple contexts and you can have as many contexts as you want as long as they make sense. If you are passing a prop down just once, does not make sense to use context. If you have more complex passing of props, context may be worth it.

(by Future ManKrzysztof WolińskiBens Steves)

參考文件

  1. Is React Context an antidote for prop drilling? (CC BY‑SA 2.5/3.0/4.0)

#react-context #reactjs #use-context






相關問題

在項目中使用 Context API 我以前會在異步操作創建者中使用 Redux? (Using Context API in a project I would have previously used Redux in - async action creators?)

Nextjs 和上下文 API (Nextjs and Context API)

從 redux-saga 函數訪問 React 上下文值 (Access React context value from redux-saga function)

如何在 Django 應用程序中用 react ContextAPI 替換 Redux (How can you replace Redux by the react ContextAPI in a Django App)

從深度嵌套的組件更新狀態而不重新渲染父組件 (Update state from deeply nested component without re-rendering parents)

更新 useContext 中的值時,組件不會重新呈現 (Component not re rendering when value from useContext is updated)

為什麼我無法從反應上下文訪問變量?你能找出錯誤嗎? (Why am I not able to access variable from react context? Can you identify the mistake?)

React Hook useEffect 缺少依賴項(在上下文中定義的函數) (React Hook useEffect has a missing dependency (function defined in context))

React Context 是道具鑽探的解毒劑嗎? (Is React Context an antidote for prop drilling?)

主題提供者問題 (Theme Provider issue)

打字稿反應上下文+類型'{}'沒有調用簽名 (Typescript react context + Type '{}' has no call signatures)

next.js socket.io 使用效果不更新套接字消息 (next.js socket.io useeffect not updating on socket message)







留言討論