4

I have a lightning web component that uses user interaction to build up some hierarchical data on client-side. When the user is finished, she hits the "Apply" button and the data is sent to an imperative apex class which inserts the data into the db.

How can I detect that the user is trying to navigate away from the page, and present a modal dialog box warning that their data will be lost if they don't save it? I think that in aura this is done with lightning:unsavedChanges. That component appears to have no lwc equivalent.

Also, this LWC is rendered on a record detail page. It will cause several of the related record lists to change. How can I cause them to be re-rendered when the user's data is applied?

2 Answers 2

1

I don't think salesforce supports this at the moment. The only thing i've been able to come up with is to create a separate aura component that uses lightning:unsavedChanges and listens to pub/sub events using this component to activate or deactivate the warning from other components on the same page. I've not tested this though.

Also, this LWC is rendered on a record detail page. It will cause several of the related record lists to change. How can I cause them to be re-rendered when the user's data is applied?

This too is not supported yet, please upvote this idea to get salesforce to fix this. A similar workaround with an aura component that fires a e.force:refreshView event on receiving a pubsub event is possible here.

-2

Working Code:

constructor() {
        super();
       window.addEventListener('beforeunload', (event) => {
  // Cancel the event as stated by the standard.
  event.preventDefault();
  // Chrome requires returnValue to be set.
  event.returnValue = 'sample value';
});
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .