SetState在承诺后返回旧价值
我将图像文件保存到S3 Amazon Web服务,然后检索链接后。 当链接从s3返回作为字符串时,请尝试将其保存到状态。但是,如果我们在Console.log(EditedProduct)中对其进行测试,则每次返回以前的值时设置了Productuct,但我需要新的。
const imageSave = async (file: File, fileName: string) => {
await modalProductUpload(file).then((s3payload) => {
setEditedProduct({ ...editedProduct, [fileName]: s3payload.Location })
console.log(s3payload.Location);
});
setTimeout(() => {
console.log(editedProduct);
}, 6000)
}
I Saved image file to s3 amazon web service, and after retrieved the link.
When link returned from s3 as a string a try to save it to state.But if we test it in console.log(editedProduct),setEditedProduct every time return the previously value, but I need the new.
const imageSave = async (file: File, fileName: string) => {
await modalProductUpload(file).then((s3payload) => {
setEditedProduct({ ...editedProduct, [fileName]: s3payload.Location })
console.log(s3payload.Location);
});
setTimeout(() => {
console.log(editedProduct);
}, 6000)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
React将异步批量设置状态。您可以在
useffect
中获得更新的状态,并具有依赖性。React would batch set state asynchronously. You could get the updated state in the
useEffect
with having dependency as that state.