自定义React-redux
实现mini版react-redux 1. 理解react-redux模块 1). react-redux模块整体是一个对象模块 2). 包含2个重要属性: Provider和connect 3). Provider 值: 组件类 作用: 向所有容器子组件提供全局store对象 使用: <Provider store={store}><Xxx/></Provider> 4). connect 值: 高阶函数 作用: 包装组件生成容器组件, 让被包装组件能与redux进行通信 使用: connect(mapStateToProps, mapDispatchToProps)(Xxx) 2. context的理解和使用 1). 理解 当你觉得多层传递props麻烦, 可以选择使用context context是组件对象的一个属性, 它的值是一个对象 一个组件指定的context内数据, 所有层次子组件都可以读取到 如果可以尽量不用context, 你可以选择使用react-redux, react-redux内部就利用了context 2). 使用 父组件: static childContextTypes = { color: PropTypes.string } getChildContext() { return {color: 'red'}; } 后代组件: static