context的使用

const {Provider, Consumer} = React.createContext(defaultValue);

<Provider value={/*共享的数据*/}>
    /*里面可以渲染对应的内容*/
</Provider>

用法1:
<Consumer>
  {value => /*根据上下文  进行渲染相应内容*/}
</Consumer>



用法2:
类上绑定静态属性。
static contextValue = Consumer




用法3:函数组件 + hooks (useContext)

const
contextValue = useContext(Consumer)

 
posted @ 2022-03-15 23:57  Action_swt  阅读(40)  评论(0编辑  收藏  举报