2017-02-26 117 views
2

我有两个按以下方式调用的redux操作。Redux操作未被第二次调用

export function action1(params) { 
    //This line is always called. 
    return (dispatch) => { 
    //This line is not called the second time. 
    return MyApi.call1(params) 
    .then(response => { 
     // some logic 
     return dispatch(someFunction1()); 
    }) 
    .catch(error => { 
     throw(error); 
    }); 
    }; 
} 

export function action2(params) { 
    return (dispatch) => { 
    return MyApi.call2(params) 
    .then(response => { 
     // call the first API again 
     action1(); 
     return dispatch(someFunction2()); 
    }) 
    .catch(error => { 
     throw(error); 
    }); 
    }; 
} 

当第一次加载视图,action1被视图的构造方法中调用。在执行操作并在相同视图中触发action2时,需要在action2的成功上调用action1以从服务器获取更新列表。不幸的是,当第二次调用action1时代码没有任何错误。

我在这里错过了什么?

+5

btw,你还没有调度action1,如:'dispatch(action1(params))'; –

+1

它的工作。这对我来说太蠢了。你可以发布这个答案,以便我可以接受吗? –

回答

2

您还没有发布过action1

dispatch(action1(params))

调用action1()没有dispatch刚刚返回的功能。为了得到返回函数dispatch,你应该使用dispatch那个函数。然后它会被redux-thunk中间件所捕获。中间件将通过dispatch并调用函数。