Axios响应拦截器与redux reducer的组合

时间:2017-01-13 13:07:03

标签: redux interceptor reducers axios

   import React from "react";
    import axios from "axios";
    import {push} from "react-router-redux";
    import {actionTypes} from "../Patient/actions";
    import {put, call} from "redux-saga/effects";
    import {updatePermissionsAction} from "../Patient/actions"

    // Add a response interceptor for getting permissions
    axios.interceptors.response.use(function(response) {

        console.log("in interceptorXXXXXXXXXXX");
        if(response.headers.permissions == null) {
            return response;
        }
        var permissions = response.headers.permissions.split(',');

        permissions.forEach((permission, index, permissionArray) => {
            permissionArray[index] = permission.trim();
        });


        put({
             type: actionTypes.UPDATE_PERMISSIONS,
             permissions: permissions
         });
        return response;
    }, function (error) {
        // Do something with response error

    });

正在调用拦截器,但是put没有效果。当我的传奇叫减速机工作正常。

也许拦截器不能使用这种模式?拦截器有没有办法调用reducer?

1 个答案:

答案 0 :(得分:0)

也许传奇可能需要导出并使用applyMiddleware添加到redux中,以便能够使用“ put”调度动作(?)。不太确定在这种情况下将如何实施。

我处于类似的情况,我可以通过导入商店来调度动作,然后用store.dispatch(myaction(data));调用该动作(我没有使用传奇)。

import {myAction} from 'actionCreators';
import store from 'store';

axios.interceptors.response.use(response => {

    if('dataProperty' in response.data){

        store.dispatch(myAction(response.data.dataProperty));

    }
    return response;

}