Redux

作者: 晴天的晴q | 来源:发表于2019-02-23 20:44 被阅读0次

store 修改数据的工作流程

注意:
1. store 是唯一的
2. 只有 store 能改变自己的内容
3. reducer 必须是纯函数
    纯函数:给定固定的输入,就一定会有固定的输出,而且不会有任何副作用

Redux 核心 API

(1)createStore:创建一个 store

(2)store.dispatch():派发 action,传递给 store

(3)store.getState():获取 store 里面所有的数据内容

(4)store.subscribe():订阅 store 的改变

react-redux

(1)核心组件:Provider(一般用于App.js包裹组件)

<Provider store={store}></Provider>,Provider 里所有的组件都有能力去使用 store 里的数据, 换句话说,Provider 把 store 里的数据都提供给了它包含的组件。

(2)connect 方法:帮助 Provider 组件包含的组件与 store 建立连接的方法

使用方法:<1> export default connect( mapStateToProps, mapDispatchToProps )( 组件名 )

connect 接收两个映射的参数:

const mapStateToProps = ( state ) => { return {} };

const mapDispatchToProps = ( dispatch ) => { return {} }

相关文章

网友评论

      本文标题:Redux

      本文链接:https://www.haomeiwen.com/subject/yuqmyqtx.html