2017-02-26 102 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並調用函數。