Redux-actions
redux 流程里有大量的样板代码读写很痛苦,使用 redux-actions 可以简化 Action 和 Reducer 的处理。
- redux-actions 下载
npm install redux-actions
- 创建 Action ```javascript import { createAction } from ‘redux-actions’;
const increment_action = createAction(‘increment’); const decrement_action = createAction(‘decrement’);
3.
创建 Reducer
```javascript
import { handleActions as createReducer } from 'redux-actions';
import { increment_action, decrement_action } from '../actions/counter.actions';
const initialState = { count: 0 };
const counterReducer = createReducer({
[increment_action]: (state, action) => ({count: state.count + 1}),
[decrement_action]: (state, action) => ({count: state.count + 1}),
}, initialState);
export default counterReducer;
- actions 传参
const handleIncrement = (state, {payload}) => ({count: state.count + payload});
const handleDecrement = (state, {payload}) => ({count: state.count - payload});