码迷,mamicode.com
首页 > 其他好文 > 详细

【14】redux 之 redux-actions

时间:2017-09-01 11:21:33      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:ret   javascrip   run   reducer   rom   res   action   ams   esc   

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写法要使用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, 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

标签:ret   javascrip   run   reducer   rom   res   action   ams   esc   

原文地址:http://www.cnblogs.com/yeziTesting/p/7462235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!