Intermediate 15 min read

NavigationMixin and Page References

By the end of this lesson, you'll be able to:

  • Explain what a mixin is and how NavigationMixin extends a component class
  • Build a PageReference object describing a navigation destination
  • Trigger navigation using this[NavigationMixin.Navigate]

Prerequisites: Module 8: "Project: A Professional Lead Capture Component"

What Is NavigationMixin?

A mixin is a function that takes a base class and returns a new, extended class with added capability — a way to add a specific trait (like navigation) without a traditional, rigid inheritance chain (Module 4 covered plain JavaScript classes; a mixin is a pattern built on top of that). NavigationMixin is Salesforce's built-in mixin for adding page-navigation capability to any component.

Applying the Mixin

import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class AccountLink extends NavigationMixin(LightningElement) {
    // this class now has navigation capability
}

Instead of extends LightningElement, the class extends NavigationMixin(LightningElement) — the mixin wraps the base class and returns an enhanced one.

PageReference Objects

const pageReference = {
    type: 'standard__recordPage',
    attributes: {
        recordId: this.accountId,
        objectApiName: 'Account',
        actionName: 'view',
    },
};

A PageReference is a plain object describing where to go — a type (the kind of page) plus attributes (and sometimes state) specific to that type. Lesson 2 covers the exact shapes for records, objects, and list views in depth.

this[NavigationMixin.Navigate]

handleViewAccount() {
    this[NavigationMixin.Navigate](pageReference);
}

NavigationMixin.Navigate is a special, imported symbol used as a computed property name to call the navigation method the mixin added to the class — an unusual-looking but standard syntax specific to this API. This actually performs the navigation using the PageReference built above.

Exercise

Apply NavigationMixin to a class named ContactLink that extends LightningElement.

Show hint

The mixin wraps the base class in the extends clause.

JAVASCRIPT

Exercise

Challenge: explain, as a comment, what a mixin is and why NavigationMixin uses this pattern instead of a normal base class to extend.

Show hint

Think about how many different "traits" a single component might need at once.

JAVASCRIPT

NavigationMixin and Page References Quiz

1. What is a mixin?

2. How is NavigationMixin applied to a component class?

3. What does a PageReference object describe?

4. What is used to actually trigger navigation once a PageReference is built?

5. What two main properties does a PageReference object typically have?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

NavigationMixin adds navigation capability to a component class without a traditional inheritance chain — a PageReference object describes the destination, and Navigate performs the move.