Dre*_*rew 1 callback reactjs redux redux-thunk
所以在我的 React 组件中,我有这个:
this.props.updateAlertCallback('error', ERROR_MESSAGE)
Run Code Online (Sandbox Code Playgroud)
我的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!');
});
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误: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();
Run Code Online (Sandbox Code Playgroud)
现在你可以调用 updateAlert(xx, xx).then(newState => {.....});
| 归档时间: |
|
| 查看次数: |
10406 次 |
| 最近记录: |