1. 程式人生 > >【14】redux 之 redux-actions

【14】redux 之 redux-actions

scrip reset case 原來 from reat sin con spa

redux-actions有兩大法寶createActionhandleActions.

createAction

http://www.jianshu.com/p/6ba5cd795077

原來創建action:

const startAction = () => ({ type: START });

使用redux-actions創建action:

import { createAction } from ‘redux-actions‘;
const startAction = createAction(START);

handleActions

原來reducer操作state寫法要使用switch

if else來匹配:

function timer(state = defaultState, action) {
  switch (action.type) {
    case START:
      return { ...state, runStatus: true };
    case STOP:
      return { ...state, runStatus: false };
    case RESET:
      return { ...state, seconds: 0 };
    case RUN_TIMER:
      return { ...state, seconds: state.seconds + 1 };
    default:
      return state;
  }
}

使用redux-actions``reducer操作state:

const timer = handleActions({
  START: (state, action) => ({ ...state, runStatus: true }),
  STOP: (state, action) => ({ ...state, runStatus: false }),
  RESET: (state, action) => ({ ...state, seconds: 0 }),
  RUN_TIMER: (state, action) => ({ ...state, seconds: state.seconds + 1 }),
}, defaultState);

http://blog.csdn.net/sinat_17775997/article/details/70176723

【14】redux 之 redux-actions