1

I'm building a community on LWR. I would like to share information between 2 pages through sessionStorage.

Page1

handleSelection(event){
   this.selectedContactId = event.target.dataset.id;
   sessionStorage.setItem("selectedContact", this.selectedContactId);
   this[NavigationMixin.Navigate]({
       type: "comm__namedPage",
       attributes: {
           name: "Dashboard__c"
        }
    });
 }

Page2

connectedCallback(){
   console.log(sessionStorage.getItem("selectedContact"));
  //output: null
}

Observations

  1. NavigateMixin redirect well from Page1 to Page2, however the sessionStorage is always returning null value
  2. If I refresh page2 manually sessionStorage works and return the right value.

Workaround

I am using window.open("./dashboard","_self");

but I would really like to use NavigationMixin which does a smoother transition between pages.

5
  • Maybe it's a race condition somehow? What if you check in the renderedCallback, or wait a tic using await Promise.resolve() in connectedCallback?
    – sfdcfox
    Commented Mar 31, 2022 at 12:08
  • hey @sfdcfox yes I tried all of those but no much results yet. Very weird. Wonder if it's a navigationmixin behaviour or something. Same thing happen for loadScript.
    – brovasi
    Commented Apr 1, 2022 at 4:26
  • Interestingly it's working with BYO(aura) template. So I guess it's LWR community issue then. Performance is amazing but still so many gaps. twitter.com/DavidBrowaeys/status/1509461899301175296
    – brovasi
    Commented Apr 3, 2022 at 11:17
  • @brovasi did you ever find a solution to this problem?
    – Rob
    Commented Oct 1, 2022 at 11:35
  • I haven't retry recently this was a big blocker for us as we use sessionStorage quite heavily. We are still on byo aura.
    – brovasi
    Commented Oct 2, 2022 at 23:54

1 Answer 1

2

I was facing the same issue. I need to open a refreshed tab whether I open it directly or through Navigation Mixin from another tab. I tried getting values from session storage in constructor, renderedCallback and connectedCallback but these functions does not called/run when we move back and forth between the tabs or when we use Navigation Mixin and didn't prefer to use window.open

So, I tried using CurrentPageReference and it resolved all of my problem.

Whether I am opening tab directly or by using Navigation Mixin, its getting correct session storage values.

You need to just import this:

import { CurrentPageReference } from 'lightning/navigation';

then write code like this:

@wire(CurrentPageReference)
    getStateParameters(currentPageReference) {
        if (currentPageReference) { 
            this.urlStateParameters = currentPageReference.state;
            if(Object.keys(this.urlStateParameters).length === 0) {
                if(sessionStorage.getItem('anyvalue')) {
                    this.yourvariable = sessionStorage.getItem('anyvalue');
                }
            }
        }
    }

Hope this solution will help you also.

You must log in to answer this question.

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