I'm working on a community where I need to show a popup if a user navigates away from their shopping cart and there are items in it. I have this built in a Lightning Web Component. I can get the pop-up to show using disconnectedCallback() but I can't stop the navigation. Does anyone know if there is a way to have the user remain on the page if they decide based on the confirm popup that they don't want to navigate away? Here's what I have so far:

   disconnectedCallback() {
            if (this.hasRegistrants || this.hasSurveys) {
                if (!confirm('Wait! You still have items in your cart. Registration is not complete. Are you sure you want to leave?')) {
                    //Can I stop the user from navigating away here?

In Aura, we have lightning:unsavedChanges. Unfortunately, we do not yet have a parallel to this in LWC. You cannot cancel or prevent this event, as it is called after the component has already moved away from the component (i.e. after is removed from the DOM). If you need this functionality, use Aura instead of LWC; you could write an Aura "wrapper" to call this component without rewriting the entire component.

  • It doesn't look like this is available for community pages. Any other ideas? – melsherm3 Apr 13 at 15:53
  • @melsherm3 didn't notice the community before. Unfortunately, there exists no solution I'm aware of at this time. – sfdcfox Apr 13 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.