0

I have a custom lwc that navigates to an aura and it's working fine but whenever I change the url parameters the onPageReference method from the aura is not working.

This is my lwc code:

        this[NavigationMixin.Navigate]({ 
            type : 'standard__component' ,
            attributes : {
                componentName : 'c__MyAuraComponent'
            },
            state : {
                c__previousPageReference : //previous page reference,                    
                c__question : //object,
                c__questionId : //id
            }
        });

And this is my aura:

cmp:

<aura:handler name="change" value="{!v.pageReference}" action="{!c.onPageReferenceChange}"/>

controller.js:

onPageReferenceChange: function(component, event, helper) {
    console.log('**reference has changed');
    helper.managePageReference(component);
},

But I'm not being able to get the console log.

Is there something I'm doing wrong?

Regards,

EDIT My aura also has a onInit handler and both onInit and onPageReferenceChange call the same helper method for data retrieve based on url:

cmp:

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:handler name="change" value="{!v.pageReference}" action="{!c.onPageReferenceChange}"/>

controller.js: onPageReferenceChange: function(component, event, helper) { console.log('**reference has changed', component.get("v.pageReference")); helper.managePageReference(component); }, doInit: function (component, event, helper) { helper.managePageReference(component); },

helper.js:

managePageReference: function (component, event) {
    var myPageRef = component.get("v.pageReference");
    if(myPageRef && myPageRef.state){
        Object.keys(myPageRef.state).forEach(key => {
            let fixedKey = key.split('c__').pop();
            component.set("v." + fixedKey, myPageRef.state[key]);
        });
    }
},
9
  • How do these two components interact? You've said that the LWC navigates to the aura component, but are you expecting the LWC to update the Aura component on navigation if that Aura component exists? Sorry, but I'm struggling to understand what you're meaning with your question. Commented Mar 29, 2023 at 8:17
  • Hi @CallumMacErlich. My lwc display a number of items and each item can navigate to the same aura but with different data that's been updated to the componente. Whenever I go back to the lwc and try picking a different item the onPageReference method doesn't launch.
    – molinet
    Commented Mar 29, 2023 at 8:25
  • Have you got an onInit method in your aura component? My understanding is that each time you navigate to the Aura component it would be a new instance of it, rather than the same one. Commented Mar 29, 2023 at 8:29
  • @CallumMacErlich Yes, I have an onInit handler and the onchange handler attached to the pageReference. onInit is working fine but onPageReference is not. I'll update the question
    – molinet
    Commented Mar 29, 2023 at 8:50
  • 1
    Thanks @CallumMacErlich! Now it's working!
    – molinet
    Commented Mar 29, 2023 at 9:23

1 Answer 1

1

Just summarizing the solution if this impacts anyone else.

The simple fix for this is to update the call to the Navigate API to pass the 'replace' argument. This means that the page state is updated without pushing a new history entry onto the browser's history stack.

this[NavigationMixin.Navigate](PageReference, true);

This is loosely detailed in the 'Add Query Parameters' LWC Developer Guide documentation.

You must log in to answer this question.

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