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

前端之家收集整理的这篇文章主要介绍了redux在react-native上使用(五)--redux-actions使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

redux-actions有两大法宝createActionhandleActions.

createAction

原来创建action:

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

使用redux-actions创建action:

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

handleActions

原来reducer操作state写法要使用switchif 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,runStatus: false }),RESET: (state,seconds: 0 }),RUN_TIMER: (state,seconds: state.seconds + 1 }),},defaultState);
原文链接:https://www.f2er.com/react/304538.html

猜你在找的React相关文章