Lightning Web Components – Using NavigationMixin to Pass State in LWC

lightning-web-componentsnavigationnavigationmixin.navigate

I am trying to call an LWC and pass the Id. However the second LWC variable is undefined.

First LWC:

this[NavigationMixin.Navigate]({
            type: 'standard__navItemPage',
            attributes: {
                apiName: 'secondLWC',
            },
            state: {
                c__recordId: 'someId...'
            }
        });

The second LWC Opens but when RenderedCallback runs "recordId" us undefined

recordId;

renderedCallback() {
    console.log('In renderedCallback');
    console.log('recordId: ', this.recordId); //undefined
}

Best Answer

You need to get recordId from the URL.

@wire(CurrentPageReference)
getStateParameters(currentPageReference) {
   if (currentPageReference) {
       this.recordId = currentPageReference.state?.c__recordId;
   }
}

More details here https://beyondthecloud.dev/blog/get-url-parameters-in-lwc

Related Topic