reactjs - 无法删除带有警报消息的记录
问题描述
我有 react-table,它是一个简单的 CRUD 应用程序。我对每一行都有一个删除按钮来删除该记录。
单击此删除按钮时,会弹出一个警告框,其中包含删除和取消选项
此警报框中的删除选项无法从表中删除记录。虽然,我可以在浏览器控制台中看到已删除的记录。好吧,那么它可能无法更新表。
我认为successDelete()
,我错误地通过了 apiInfo: this.state.apiInfo.filter((ai, prop) => ai.uuid !== prop.uuid),
. 但是,现在确定如何纠正它。
完整的代码可以在代码沙箱中找到。以下是相关的代码片段。
状态变量:
this.state = {
isLoading: true,
apiInfo: [],
alert: null,
};
负责它的职能:
warningWithConfirmAndCancelMessage = () => {
this.setState({
alert: (
<ReactBSAlert
warning
style={{ display: "block", marginTop: "-100px" }}
title="Are you sure?"
onConfirm={() => this.successDelete()}
onCancel={() => this.cancelDetele()}
confirmBtnBsStyle="success"
cancelBtnBsStyle="danger"
confirmBtnText="Yes, delete it!"
cancelBtnText="Cancel"
showCancel
btnSize=""
>
You will not be able to recover this imaginary file!
</ReactBSAlert>
),
});
};
successDelete = () => {
this.setState({
alert: (
<ReactBSAlert
success
style={{ display: "block", marginTop: "-100px" }}
title="Deleted!"
onConfirm={() => this.hideAlert()}
onCancel={() => this.hideAlert()}
confirmBtnBsStyle="success"
btnSize=""
>
Your imaginary file has been deleted.
</ReactBSAlert>
),
apiInfo: this.state.apiInfo.filter((ai, prop) => ai.uuid !== prop.uuid),
});
};
cancelDetele = () => {
this.setState({
alert: (
<ReactBSAlert
danger
style={{ display: "block", marginTop: "-100px" }}
title="Cancelled"
onConfirm={() => this.hideAlert()}
onCancel={() => this.hideAlert()}
confirmBtnBsStyle="success"
btnSize=""
>
Your imaginary file is safe :)
</ReactBSAlert>
),
});
};
hideAlert = () => {
this.setState({
alert: null,
});
};
然后在渲染内部的 return() 中:
<div className="content">
{this.state.alert}
<Button
fill="true"
onClick={this.warningWithConfirmAndCancelMessage}
color="danger"
size="sm"
className="btn-icon btn-link remove"
id="tooltip974171201"
>
<i className="fa fa-times" />
</Button>
</div>
解决方案
你需要调用deleteAdminAPInfo
你的successDelete
方法。
由于deleteAdminAPIInfo
方法是async
它将返回给您一个承诺。因此,您可以将 a 链接then
到它以显示成功弹出窗口,如下所示:
successDelete = () => {
this.deleteAdminAPInfo().then(() => {
this.setState({
alert: ( <
ReactBSAlert success style = {
{
display: "block",
marginTop: "-100px"
}
}
title = "Deleted!"
onConfirm = {
() => this.hideAlert()
}
onCancel = {
() => this.hideAlert()
}
confirmBtnBsStyle = "success"
btnSize = "" >
Your imaginary file has been deleted. <
/ReactBSAlert>
)
});
});
};
此外,由于对DELETE
API 的调用似乎返回了已删除的数据,因此您还必须将其从前端的状态数据中过滤掉。
为此,您可以在您的deleteAdminAPInfo
方法中执行以下操作:
deleteAdminAPInfo = async () => {
console.log("get partner api info ----");
await axios
.delete("https://run.mocky.io/v3/de5b1f30-7150-42de-8c58-3729c7ee0b88")
.then((response) => {
console.log("get partner api info - api response:", response.data);
this.setState({
isLoading: false,
apiInfo: this.state.apiInfo.filter(
(info) => response.data.findIndex(item => item.uuid === info.uuid) < 0
)
});
})
.catch((error) => {
// handle error
if (axios.isCancel(error)) {
console.log("getmdi-Unable to fetch measurementsData", error.message);
} else {
this.setState({
isLoading: true
});
}
});
};
这是您参考的更新的 CodeSandbox。
希望这可以帮助。
推荐阅读
- java - @JsonInclude(Include.NON_EMPTY) 在使用 CUSTOM SERIALIZER 时不起作用
- php - 用链接查找和替换关键字,但忽略 div 内容
- visual-studio - mvtnorm 无法在 MS r-client 版本 3.4.3 中加载
- asp.net - 如何使用 signalR 将我的网络摄像头流式传输给网站上的用户
- amazon-web-services - 尝试使用 LetsEncrypt 安装 SSL 证书
- database - 两个 PostgreSQL 模式之间的差异以更新脚本
- go - 如何使用 godoc 和 go 模块提供文档?
- mysql - 基于所有具有相同值的辅助列检索列
- python - 使用 TkInter 在 Python 中创建菜单时出错。我究竟做错了什么?
- android - 即使包含值,Rect.contains() 也不会返回 true