Redux解决了什么问题

忽略此文

Redux是什么?

Redux是Facebook提出一个数据状态管理的库,也可以说框架。它搭配React解决了组件之间通信问题,这个通信问题,是组件和其它组件之间也可以跨层通信,不需要一层一层的把,父组件的数据往下传递。这会增加代码复杂度和维护的复杂度。

如果使用React.createContext的API也可以实现跨层通信,你可以单独写一个模块去封装Context.Provider和Context.Consumer,只有在根组件使用Context.Provider后,需要共享Context上的数据的组件,都可以通过引入Context.Consumer组件去获取公共的数据。哪个组件需要顶层组件的数据,就必须引入Context.Consumer。

  //context.js
  export default Context  = createContext('')
 //app.js
 let store = createStore(reducer)
 <Context.Provier value={store}>
    <App />
 </Context.Provider>

 import Context from './context.js'
 <Context.Consumer>
    {(data) => (<div>{data.time}</div>)}
</Context.Consumer>

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理