Redux-actions

redux 流程里有大量的样板代码读写很痛苦,使用 redux-actions 可以简化 Action 和 Reducer 的处理。

  1. redux-actions 下载
    1. npm install redux-actions
  1. 创建 Action ```javascript import { createAction } from ‘redux-actions’;

const increment_action = createAction(‘increment’); const decrement_action = createAction(‘decrement’);

  1. 3.
  2. 创建 Reducer
  3. ```javascript
  4. import { handleActions as createReducer } from 'redux-actions';
  5. import { increment_action, decrement_action } from '../actions/counter.actions';
  6. const initialState = { count: 0 };
  7. const counterReducer = createReducer({
  8. [increment_action]: (state, action) => ({count: state.count + 1}),
  9. [decrement_action]: (state, action) => ({count: state.count + 1}),
  10. }, initialState);
  11. export default counterReducer;
  1. actions 传参
    1. const handleIncrement = (state, {payload}) => ({count: state.count + payload});
    2. const handleDecrement = (state, {payload}) => ({count: state.count - payload});