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);

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...