componentWillUnmount and setState to open Dialog to confirm a save of data

205 Views Asked by At

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?

0

There are 0 best solutions below