Sub topic 10
Reading the current page with CurrentPageReference
This chapter assumes you can build a basic Lightning Web Component and use @wire. You should also know what a PageReference is from the earlier navigation chapters: an object with a type, attributes, and state.
What the adapter gives you
NavigationMixin.Navigate sends users to a page. CurrentPageReference does the reverse. It is a wire adapter from lightning/navigation that provides the PageReference of the page the user is on now.
The value has the same three parts you pass to Navigate:
typenames the kind of page, such asstandard__recordPage.attributesidentify the target, such asrecordIdorobjectApiName.stateholds URL parameters, such asc__custom parameters or list view filters.
Wiring it to a function
import { LightningElement, wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
export default class WhereAmI extends LightningElement {
currentPageReference;
@wire(CurrentPageReference)
setPageRef(pageRef) {
this.currentPageReference = pageRef;
}
}The wire service calls setPageRef once when the component connects. It calls it again whenever the page reference changes. Assigning the value to a field lets the template and other getters react to it.
You can also wire it to a property, but a function gives you a place to run logic each time the value changes.
Reading values from it
Use the PageReference to read the type, attributes, or state. Guard against undefined, because the wire can provide no value before the first result.
import { LightningElement, wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
export default class PageInfo extends LightningElement {
pageType;
recordId;
source;
@wire(CurrentPageReference)
handlePageRef(pageRef) {
if (!pageRef) {
return;
}
this.pageType = pageRef.type;
this.recordId = pageRef.attributes.recordId;
this.source = pageRef.state ? pageRef.state.c__source : undefined;
}
}This works well for a component that must behave differently depending on where it sits, or that reads a custom parameter passed by another component. A sender can add that parameter by setting state: { c__source: 'dashboard' } in its Navigate call.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| `pageRef` is `undefined` | The wire has not returned a value yet | Check `if (!pageRef)` before reading properties |
| `attributes.recordId` is `undefined` | The current page is not a record page | Check `pageRef.type` first |
| A state value is missing | The key has no `c__` prefix | Add the prefix on both the sender and reader |
| Treating the value as writable | The PageReference is read-only | Use `Navigate` with a new PageReference to change the page |
Exercise
A component reads pageRef.attributes.recordId inside its CurrentPageReference wire function and throws an error on the first call. Name two checks that prevent this.
Show answer
First, check that pageRef is defined, because the wire can call the function before a value exists. Second, check that pageRef.type is a record page type such as standard__recordPage, because other page types may not have recordId in attributes.