文章目錄
-
Heres how React’s New Context API Works - YouTube
- 要解決 props drilling 的問題
- 要用的 資料(state)由 Provider 提供給 Consumer
const Mycontext = React.createContext()
Mycontext.Provider
allow us to declare the data that we want available throughout our component tree
Mycontext.Consumer
allow any compnent in the component tree that requires that data to be able to subscribe to it.
React.useContext
Mycontext.Consumer 的寫法比較麻煩,用 useContext 增加 code 可讀性。 看這邊範例:https://youtu.be/rFnfvhtrNbQ?t=564
額外主題
Four Reasons React Renders (and three ways to stop it) – Cory House