【发布时间】:2021-03-08 10:49:52
【问题描述】:
当我将 Axios 与 async 和 await 一起使用时,如何立即更新状态数据?似乎没有立即更新状态数据。非常感谢并非常感谢。这是代码示例:
const[dbdata,setDBData] = useState([])
useEffect(async() => {
const response = await Axios.get('http://localhost:5000/api/posts/allpost', {withCredentials:true})
setDBData(response.data)
}, [])
const GetPost = async(id) =>{
const response = await Axios.put('http://localhost:5000/api/posts/getPost',{postId:id}, {withCredentials:true})
const getData = dbdata.map(item => {
if(item._id==response._id){
return response
}
else{
return item
}
})
console.log(getData)
setDBData(getData)
}
【问题讨论】:
-
stackoverflow.com/questions/64982687/… - 这能回答你的问题吗?
-
做console.log并检查
-
不要使用异步回调 useEffect(async()