# How do you navigate between pages and records in LWC using the lightning/navigation service?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Navigation, NavigationMixin

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-navigate-between-pages-and-records-in-lwc-using-the-lightning-navigation-service

## Answer

**Basics:** Hard-coded URLs break across contexts (Lightning Experience, mobile, Experience Cloud). A page reference is a platform-neutral description of where to go, and the navigation service converts it to the right URL.

**How it works:**
1. `import { NavigationMixin } from 'lightning/navigation';`
2. `export default class X extends NavigationMixin(LightningElement)`.
3. Call `this[NavigationMixin.Navigate]({ type, attributes, state })` to navigate, or `this[NavigationMixin.GenerateUrl](ref).then(url => ...)` to get a link for an anchor.
4. Common types: `standard__recordPage` (`actionName`: view, edit, clone), `standard__objectPage` (`actionName`: home, list, new), `standard__namedPage` (for example `home`), `standard__navItemPage` (custom tab), `standard__webPage` (external URL), `standard__app`, `standard__recordRelationshipPage`, and `comm__namedPage` in Experience Cloud.
5. To read incoming parameters, wire `CurrentPageReference` and read `state` (custom parameters must be prefixed `c__`).

**Advanced / deep dive:** `state` carries URL parameters for list view filters (`filterName`) or custom values (`c__orderId`). For prefilled new records use `actionName: 'new'` with `defaultFieldValues` encoded by `encodeDefaultFieldValues` from `lightning/pageReferenceUtils`. The optional second argument `true` replaces the current history entry rather than adding one. LWCs cannot be navigated to directly with `standard__component`, which targets Aura components; navigating to an LWC means putting it on a Lightning page or tab (`standard__navItemPage`) or wrapping it. Experience Cloud sites support a different set of page types and URL formats, so verify per site type. Navigation from inside a Flow screen uses the flow navigation events (`FlowNavigationNextEvent`) rather than the navigation mixin.

**Code / config example:**
```javascript
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class Nav extends NavigationMixin(LightningElement) {
    @api recordId;

    goToRecord() {
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: { recordId: this.recordId, objectApiName: 'Account', actionName: 'view' }
        });
    }
    goToList() {
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: { objectApiName: 'Contact', actionName: 'list' },
            state: { filterName: 'Recent' }
        });
    }
}
```

**Hidden gotchas & must-remember points:**
- The mixin wraps the base class; forgetting it causes `NavigationMixin.Navigate` to be undefined.
- Custom state parameters must start with `c__` or they are dropped.
- `standard__component` is for Aura components, not directly for LWC.
- Page reference types differ in Experience Cloud (`comm__*`).
- Hard-coded relative URLs and `window.location` break in mobile and different containers.
- `GenerateUrl` returns a Promise, so resolve it before using the URL.

**Best practice:** Always navigate through page references so the same code works across desktop, mobile and sites.

---
Source: ForceChallenges · last updated 2026-11-02
