【发布时间】:2020-03-08 13:09:13
【问题描述】:
我正在尝试立即更新反应原生的布尔状态变量。但是 await 关键字不起作用。因为状态设置器函数是异步的,我如何使用 async/await 来做到这一点?在vscode中,setLike setter函数前面的await有一条信息:“await has no effect on this type expression”
const likeIt = async () => {
console.log('like pressed');
console.log('liked? before set', like); //**false**
await setLike(like => !like);
console.log('liked? after set', like); //**should be true but getting false**
const axiosAuth = await axiosWithAuth();
const userToken = axiosAuth.defaults.headers.Authorization;
if (like) {
axiosAuth.post(`https://url`,{})
.then(res => {
console.log('response from post like: ', res.data);
})
.catch(err => console.log('error in post like', err.message))
} else {
axiosAuth.delete(`https://url`)
.then(res => console.log('res from unlike', res.data))
.catch(err => console.log('err from unlike', err))
}
}
【问题讨论】:
标签: reactjs react-native async-await