redux在react-native上使用(五)--redux-actions使用

redux-actions有兩大法寶createActionhandleActions.redux

createAction

原來建立action:code

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

使用redux-actions建立action:it

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

handleActions

原來reducer操做state寫法要使用switchif else來匹配:io

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`:function

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);
相關文章
相關標籤/搜索