我的子组件中有这个函数,用于删除文档并使用setDocumentList
更新它的状态。但是当我非常快地点击删除2-3个文档时,它是从初始状态过滤的,我没有在newDocList
中获得更新的状态,所以我的旧文档保持原样。并且仅为最后一个文档更新UI,因为更新状态需要时间。所以..。如何处理这种情况,请提出任何解决方案
提前感谢
const deleteFile = (doc) => {
Axios.delete(`url`).then((resp) => {
let newDocList = [...documentList];
newDocList = newDocList.filter(e => e.awsId != doc.awsId)
setDocumentList(newDocList)
if (props.onDocumentDeleted) props.onDocumentDeleted(newDocList)
}).catch(error => {
addMessageToDocUpload(doc, "Unable to delete file!", 'error')
});
}
发布于 2021-05-27 21:00:37
在设置依赖于先前状态的状态时,请尝试使用回调参数:
const deleteFile = (doc) => {
Axios.delete(`url`).then((resp) => {
setDocumentList(prevDocList => prevDocList.filter(e => e.awsId != doc.awsId));
}).catch(error => {
addMessageToDocUpload(doc, "Unable to delete file!", 'error')
});
更新新文档后,将useEffect添加到派生计算中:
useEffect(
() => {
if (props.onDocumentDeleted) { props.onDocumentDeleted(newDocList) }
},
[documentList]
);
https://stackoverflow.com/questions/67722509
复制相似问题