0

I am trying to find a way to pass data when navigating from page 1 to page 2 using NavigationMixin.Navigate. Below is the sample code that is part of a POC:

Page 1: HTML

<template>
    <lightning-button variant="brand" label="Go To Next Page" onclick={navigateToNextPage} class="slds-m-left_medium"> </lightning-button>
</template>

JS:

import { LightningElement, wire, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class Navigator_firstpage extends NavigationMixin(LightningElement) {
    @api someRecIds = [];
    
    navigateToNextPage(){
        for(let i=0; i<10; i++){
            this.someRecIds.push(i);
        }
        let storeState = this.someRecIds;
        this[NavigationMixin.Navigate]({
            type: 'standard__navItemPage',
            attributes: {
                apiName: 'second_page'
            },
            state: {
                filterName: 'xyz' 
            }
        }); 
    }
}

2nd Page:

<template>
    <lightning-card>
        This is 2nd page
    </lightning-card>
</template>

JS

import { LightningElement } from 'lwc';
import {CurrentPageReference} from 'lightning/navigation';
export default class Second_page extends LightningElement {
   // Is there a way to read the data from the state that was passed from the previous page?
   // I could not get the state in CurrentPageReference
   // OR is there any better way to pass the array when navigating?
}

2 Answers 2

0

I don't think it's possible at the moment. I usually have my components in a wrapper component and hide/display, instead of navigating, using if templates in HTML. And I pass the data from one component to the other via events and event handlers.

Note: You are reassigning the value of public property and should be avoided.

0

on the second page do this

 @wire(CurrentPageReference)
    setCurrentPageReference(currentPageReference) {
        this.currentPageReference = currentPageReference;
        if (this.currentPageReference) {
           this.filterName = this.currentPageReference.state.filterName;
    }
    }

see if it helps.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.