Context理解

官方文档

https://doc.react-china.org/docs/context.html#api

个人理解

context的使用用到生产者消费者模式,React.createContext创建一对 { Provider, Consumer },Provider需要一个value属性,这个属性能够传递给 Provider 的后代 Consumers。

export const themes = {
  light: {
    foreground: '#ffffff',
    background: '#222222',
  },
  dark: {
    foreground: '#000000',
    background: '#eeeeee',
  },
};
//export 是将该变量导出,这样在其他文件中可以通过import导入
export const ThemeContext = React.createContext(
  themes.dark // 默认值
);

function ThemedButton(props) {
  return (
//使用Context.Consumer
    
      {theme => (
        

你可能感兴趣的:(Context理解)