Advanced 18 min read

State Management and Error Handling Strategy

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

  • Identify where different kinds of state should live across a component tree
  • Design a consistent, reusable error-handling strategy
  • Recognize why inconsistent error handling becomes a real problem at scale

Prerequisites: "Controller/Service/Selector Patterns for LWC"

Where State Lives

  • Local component state — a property that only that one component cares about.
  • Parent-child shared state — passed down via @api properties, communicated back up via CustomEvent (Module 5).
  • Unrelated-component shared state — Lightning Message Service (Module 5), for components with no direct parent-child relationship.

Unlike some single-page-app frameworks, LWC has no built-in global state store by default — state is deliberately kept close to where it's needed, using the mechanisms above rather than one giant shared object. This makes deliberately choosing where a given piece of state belongs more important, not less.

A Consistent Error-Handling Strategy

// errorUtils.js — one shared pattern used everywhere
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export function reduceErrorMessage(error) {
    return error?.body?.message ?? 'An unexpected error occurred.';
}

export function showErrorToast(component, error, title = 'Error') {
    component.dispatchEvent(new ShowToastEvent({
        title,
        message: reduceErrorMessage(error),
        variant: 'error',
    }));
}

Rather than each component inventing its own ad hoc error-handling approach, a small, shared, importable module (Module 4's export/import) standardizes it — every component calls showErrorToast(this, error) and gets the same consistent behavior, rather than one showing a toast, another a console log, and a third silently swallowing the error entirely.

Why This Matters at Scale

Inconsistent error handling across dozens of components in a real org becomes a genuine maintenance and user-experience problem — users see different, unpredictable feedback for the same class of failure depending on which component happened to hit it, and developers waste time re-inventing the same handling logic differently each time.

Exercise

Refactor this component's error handling to use the shared showErrorToast utility instead of its own inline toast dispatch.

Show hint

Import from errorUtils and call showErrorToast(this, error).

JAVASCRIPT

Exercise

Challenge: explain, as a comment, why LWC not having a built-in global state store makes deliberate state placement more important, not less.

Show hint

Think about what fills the gap when there's no single shared store to default to.

JAVASCRIPT

State Management and Error Handling Strategy Quiz

1. What mechanism handles shared state between unrelated components with no direct parent-child relationship?

2. Does LWC provide a built-in global state store by default?

3. What problem does a shared error-handling utility solve?

4. What communicates state from a child component back up to its parent?

5. Why does inconsistent error handling become a real problem at scale?

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

LWC has no built-in global state store, which makes being deliberate about where state lives — and how errors are surfaced — a genuine architectural decision, not an afterthought.