合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 如何定义 context 数据 * const Context = React.createContext(默认值) * <Context.Provider value={需要传递的数据}> </Context.Provider> ### 如何接收 context 数据 * 在子组件或者孙组件中 ``` 1. static contextType = ThemeContext; 2. const theContextValue = useContext(ThemeContext) 3. <Context.Consumer> 需要消费数据的地方 </Context.Consumer> ``` ### 使用 context 的注意点 * 使用context会导致组件复用性比较差 * 组合组件(把子组件当属性传递下去),也可以解决层层传递属性的问题 * 多个 Provider 也可以嵌套使用,里层的会覆盖外层的数据。 * `Context`只考虑偏静态数据的跨组件层级传递和共享,不考虑状态更新。