componentwillunmount和setState打开对话框以确认数据的保存

发布于 2025-02-05 05:03:13 字数 651 浏览 2 评论 0原文

我目前有一个专门用于检查用户是否要保存其数据的组件Willunmount(如果尚未保存)。 我使用材料UI的对话框创建了一个自定义提示,该对话框是通过状态更改打开的。 在我简单地使用一个窗口之前。

 componentWillUnmount = async () => {
    if (this.documentChanged !== false) {
        if (this.locationState.document_format_id === 2 || this.locationState.document_format_id === 1) {
            this.promptActionTracker = "component_exit";
            this.dialogTitle = "Exiting Component";
            this.dialogText = "Do you wish to save document data?";
            this.errorType = "information";
            this.setState({dialogOpen: true});
        }
    }
};

我该如何打开提示,让用户单击“确定”或“取消”,处理该操作,然后卸载组件?

I currently have a componentWillUnmount used specifically to check if a user wants to save their data, if it has not been saved already.
I created a custom prompt using Material UI's dialog, which is opened via a state change.
Before I was simply using a window.confirm, but now that i want to use the custom prompt, I cannot, due to componentWillUnmount being a final render in the process.

 componentWillUnmount = async () => {
    if (this.documentChanged !== false) {
        if (this.locationState.document_format_id === 2 || this.locationState.document_format_id === 1) {
            this.promptActionTracker = "component_exit";
            this.dialogTitle = "Exiting Component";
            this.dialogText = "Do you wish to save document data?";
            this.errorType = "information";
            this.setState({dialogOpen: true});
        }
    }
};

How do I go about opening the prompt, letting the user click "Ok" or "Cancel", process that action, and only then unmount the component?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文