加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:29:08 所属栏目:百科 来源:网络整理
导读:redux-actions 有两大法宝 createAction 和 handleActions . createAction 原来创建 action : const startAction = () = ({ type: START }); 使用 redux-actions 创建 action : import { createAction } from 'redux-actions';const startAction = createAct

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读