【发布时间】:2020-03-20 14:09:28
【问题描述】:
如何在 react 异步调用中调度 redux 函数。当我调用调度函数dispatch(updatingcontact() 时,我收到未定义调度的错误。
const UpdateContact = async (URL, method, type, address) => {
dispatch(updatingcontact()
const APIResponse = await fetch(URL, {
method: POST,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
"webContacts": {
"address": address
}
})
})
.then(response => {
if (!response.ok) {
return Promise.reject(response.statusText);
}
return response.json();
})
.then(status => {
return status
})
.catch(error => {
console.log(error);
});
}
我只想调用UpdateContact里面的updatingcontact()函数,调用reducer在UI中显示更新消息。
function updatingcontact() {
return {
type: ACTIONTYPES.UPDATING_CONTACT
}
}
【问题讨论】:
-
你可以使用redux thunk
-
@HMR 我在使用
return (dispatch) => {dispatch(updatingcontact())}时收到此错误Syntax error: await is a reserved word。 -
将此作为异步中间件检查出来HERE
标签: reactjs asynchronous redux middleware