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
@apiproperties, communicated back up viaCustomEvent(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).
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.
State Management and Error Handling Strategy Quiz
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.