Redux中的actions
来源:互联网 发布:java 读取日志文件 编辑:程序博客网 时间:2024/05/22 21:10
1. Action定义
Actions是中包含的是我们发送到store中的数据,或者触发store中的数据更新,而且在redux中actions是store中数据更新的唯一来源。我们使用store.dispatch()来发送actions。
下面给出一个action的示例:
const ADD_TODO = 'ADD_TODO'{ type: ADD_TODO, text: 'Build my first Redux app'}
- 由示例我们可以看出,action必须定义为JavaScript对象,其中:
- type:该字段是必须存在的,用来描述action的类型,而且该字段的值在整个应用中必须是唯一的。
- text:该字段是可选的,用来描述传递到store中的数据。
当然,除了type之外,你可以将action定义成任何你想要的结构。但是我们最好还是依据 Flux Standard Action来定义action。
2. Flux Standard Action
- 在Flux标准中action的定义如下:
- 一个JavaScript基本对象。
- 必须含有type属性。
- 可以包含error属性。
- 可以包含payload属性。
- 可以包含meta属性。
action不能包含除了type, payload, error和meta之外的任何属性。
3. redux-actions
为了创建出更加符合标准的action,我们可以借助redux-actions来完成。
使用方式为: createAction(type, payloadCreator = Identity, ?metaCreator)
其中,payloadCreator必须是方法、undefined或者null。如果payloadCreator为undefined或null,则默认为一个恒等方法。
例如:
let increment = createAction('INCREMENT', amount => amount);// same asincrement = createAction('INCREMENT');expect(increment(42)).to.deep.equal({ type: 'INCREMENT', payload: 42});
更多信息参照redux-actions。
0 0
- Redux中的actions
- redux在react-native上使用(五)--redux-actions使用
- React Native之Redux使用详解之Actions(29)
- ryu中的Instructions和Actions
- Redux中的bindActionCreators
- Redux中的Reducers
- Redux中的Store
- redux中的connect函数
- redux中的combineReducer学习
- react-redux中的<Provider>
- React+Redux中的MVC
- Redux中的bindActionCreators
- Redux中的bindActionCreators
- actions
- Actions
- Actions
- Actions
- Actions
- PAT 乙级 1027. 打印沙漏(20) Java版
- Android Studio 的一些设置
- UVa-202
- escape,encodeURI,encodeURIComponent的区别
- ADC的有效位数和信纳比计算
- Redux中的actions
- C 语言中&&,||,&,|的区别
- js 判断元素在数组中和取反操作
- 逆波兰表达式,走一波
- 论文笔记(一)Deep Ranking for Person Re-Identification via Joint Representation Learning
- Linux学习之:创建逻辑卷管理(LVM)
- Dubbo整合Spring时jar包冲突问题
- 自然语言处理编程方面小笔记
- 解決 centos -bash: vim: command not found