Redux中的bindActionCreators
来源:互联网 发布:android精彩编程200例 编辑:程序博客网 时间:2024/06/07 00:02
简要介绍:Redux中的bindActionCreators,是通过dispatch将action包裹起来,这样可以通过bindActionCreators创建的方法,直接调用dispatch(action)(隐式调用)。
主要用处:一般情况下,我们可以通过Provider将store通过React的connext属性向下传递,bindActionCreators的唯一用处就是需要传递action creater到子组件,并且改子组件并没有接收到父组件上传递的store和dispatch。
1.bindActionCreators的参数
let newAction = bindActionCreators(oldActionCreator,dispatch)
- 1
来看一下形参所表示的意思:
(1)形参oldActionCreator.
这个参数就是创建的action的集合:
//action.jsfunction action1(){ return { type:'type1' }}function action2(){ return { type:'type2' }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
import * as oldActionCreator from './action.js'let newAction = bindActionCreators(oldActionCreator,dispatch)
- 1
- 2
- 3
- 4
从上述的例子中我们可以看到oldActionCreator的形式为key:function的形式,其中function必须返回一个action(包含type标识的唯一对象)。
(2)形参dispatch
这里的dispatch,等同于store中的store.dispatch,用于组合action
2.最后结果
<child {...newAction}></child>
- 1
我们将组合oldAction和dispatch的对象传递给子组件,在子组件中,调用newAction.action1,相当于实现了dispatch(action1)。于是我们就实现了在没有store和dispatch组件中,如何调用dispatch(action)
后面发现这个接口并没有什么用,因为一般都会import react-redux…. 目前还未发现bindActionCreators的用处,估计唯一的用处就是在子组件未察觉redux的情况下,将dispatch传递给子组件。
- Redux中的bindActionCreators
- Redux中的bindActionCreators
- Redux中的bindActionCreators
- Redux总结4:bindactioncreators的用法
- redux的reateStore,combineReducers,bindActionCreators,applyMiddleware源码分析
- Redux中的actions
- Redux中的Reducers
- Redux中的Store
- redux中的connect函数
- redux中的combineReducer学习
- react-redux中的<Provider>
- React+Redux中的MVC
- react-redux中的数据传递
- Redux
- Redux
- Redux
- redux
- redux
- linux 下shell编程之for循环
- 测试开发-RESTful 架构详解
- 物理层的基本概念
- MyEclipse 8.5的优化,加快打开打开和编辑的速度
- 关于log4j2 RollingRandomAccessFile记录日志问题
- Redux中的bindActionCreators
- java基础-volatile关键字
- 17.1 内容协商技术
- 【Windows--Python3】Ping IP
- WinDbg学习笔记二
- Schwarz不等式 三角不等式
- Git branch
- java Collection容器(集合类)
- JNDI加密用户名密码