Add Query Parameters
To add query parameters to the URL, update the PageReference.state property. The navigation service uses a PageReference to generate a URL. The key-value pairs of the state property are serialized to URL query parameters. The query parameters describe the page and form a more specific URL that the user can save or bookmark.
Keep these behaviors in mind when working with the state property.
- The
PageReferenceobject is frozen, so you can’t change it directly. To navigate to the same page with a modifiedstate, copy the currentPageReferenceand modify the copy usingObject.assign({}, pageReference). stateproperties must use a namespace prefix followed by two underscores,__. If the component isn’t part of a managed package, usecfor the namespace prefix. If the component is part of a managed package, use the package's namespace.- Since the key-value pairs of
PageReference.stateare serialized to URL query parameters, all the values must be strings. - Code that consumes a value from
statemust parse the value into its proper format. - To delete a value from the
stateobject, set it asundefined. - Even when using HTTPS, including personal data for URL parameters isn’t safe. See Storing Sensitive Data for details.
- In Lightning Experience and Experience Builder sites that are built with Aura or LWR templates, the view isn’t rerendered when only the URL query string changes. To react to a change in the URL query string, we recommend that components observe the
CurrentPageReferenceobject and compare against the page reference'sstate.
The component in this example has a link labeled Show Panel that changes to Hide Panel when clicked. Clicking either link updates the current page's state. When the page state changes, the this.currentPageReference property, decorated with @wire(CurrentPageReference), updates. The component rerenders to show or hide the panel and update the link label.