微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

异步redux中间件redux-order

redux-order

redux-order是处理redux的中间件,简化redux的异步流控制处理。

安装

npm install redux-order

引入redux-order

import {createStore,applyMiddleware,compose} from 'redux';
import reduxOrder from 'redux-order';
import reducers from './reduces';

const enhancer = compose(
  //引入中间件
  applyMiddleware(reduxOrder())
);

const store = createStore(
  reducers,enhancer
);

export default store;

reduces中处理异步

// action
const LOGIN = 'auth/LOGIN';
const LOGIN_SUCCESS = 'auth/LOGIN_SUCCESS';
const LOGIN_FAIL = 'auth/LOGIN_FAIL';

const initialState = {};
// reducer
export default function reducer(state = initialState,action = {}) {
  switch (action.type) {
    case LOGIN:
      console.log(action);
      return {
        ...state,requesting: true,requested: false,};
    case LOGIN_SUCCESS:
      console.log(action);
      return {
        ...state,requesting: false,requested: true,auth: action.res
    };
    case LOGIN_FAIL:
      console.log(action);
      return {
        ...state,loginError: action.err
    };
    default:
      return state;
  }
}
// 触发 action
export function login(user,pass) {
  return {
    types: [LOGIN,LOGIN_SUCCESS,LOGIN_FAIL],promise: axios.post('/api/login',{user,pass}),data: 'message',list: [1,2,3,4]
  };
}

异步action

  • 在上面的例子里,我们创建了异步的redux请求。例子中定义了LOGINLOGIN_SUCCESSLOGIN_FAIL三个action,依次代表请求发出、请求成功、请求失败。在发出一个异步promise请求后,首先触发了LOGIN,假如请求成功则进入LOGIN_SUCCESS,否则就进入LOGIN_FAIL
  • 异步动作需要定义types,types为数组并且至少需要两个action(发出请求和结果),promise参数为异步请求,异步请求必须为promise对象。
  • 请求的结果在reducer中的action对象中获得,如果是成功的返回结果为action.req,失败的则是action.err
  • 在发出action时,还可以携带payload。可以自定义需要携带的参数,在reducer中即可访问action携带的参数。

同步action

export function logout() {
  return {
    type: logoUT
  };
}

上面为触发一个同步的action,type 参数定义要触发的动作,同样也可以携带payload。注意⚠️异步的action参数为types,而同步的为type

GitHub 地址

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐