所以在我的 React 组件中,我有这个:
this.props.updateAlertCallback('error', ERROR_MESSAGE)
我的 updateAlertCallback
操作是:
export const updateAlert = (alert, message) => {
return {
type: 'UPDATE_ALERT',
alert,
message
}
}
export const updateAlertCallback = (alert, message) => {
return dispatch => {
return dispatch(updateAlert(alert, message)).then(() => {
console.log('Done!');
});
}
}
我收到以下错误:Uncaught TypeError: dispatch(...).then is not a function
在 updateAlert
运行完成后记录内容的正确方法是什么?
最佳答案
使用 redux-thunk,你可以让 action 返回一个 promise:
export const updateAlert = (alert, message) => (dispatch, getState) => {
dispatch ({
type: 'UPDATE_ALERT',
alert,
message
});
return Promise.resolve(getState());
// or just Promise.resolve();
现在你可以调用 updateAlert(xx, xx).then(newState => {.....});
关于reactjs - 发送 Action 后的 Redux Thunk 回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39049802/