React context API

React context API

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

Reasons React renders

最後更新 Mar 30, 2022 15:59 +0800