Redux 动作没有被第二次调用
Redux action not getting called the second time
我有两个 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
时,代码没有任何错误地中断。
我在这里错过了什么?
您还没有发送action1
。
dispatch( action1( params ) )
在没有 dispatch
的情况下调用 action1()
只是 returns 一个函数。为了在返回的函数中得到 dispatch
,你应该 dispatch
该函数。然后会被redux-thunk
中间件捕获。中间件将传递 dispatch
并调用函数。
我有两个 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
时,代码没有任何错误地中断。
我在这里错过了什么?
您还没有发送action1
。
dispatch( action1( params ) )
在没有 dispatch
的情况下调用 action1()
只是 returns 一个函数。为了在返回的函数中得到 dispatch
,你应该 dispatch
该函数。然后会被redux-thunk
中间件捕获。中间件将传递 dispatch
并调用函数。