Skip to content
Back to skills

Lwc State Management

ASecurity

Share state across LWCs using pub/sub, Lightning Message Service, @wire, and reactive stores. NOT for in-component reactivity — use lwc/lwc-reactive-state-patterns.

  • 15 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 6, 2026
ai-agentsgoreactspringgitapi

Works with

  • api

Security analysis

A100/100

Pro scans all 7 files and shows the line behind each finding

Scanned September 6, 2026

npx -y skills add PranavNagrecha/AwesomeSalesforceSkills --skill lwc-state-management --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Lwc State Management?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Lwc State Management
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/pranavnagrecha-lwc-state-management/badge)](https://www.skillsdirectory.com/skills/pranavnagrecha-lwc-state-management)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: lwc-state-management
description: "Share state across LWCs using pub/sub, Lightning Message Service, @wire, and reactive stores. NOT for in-component reactivity — use lwc/lwc-reactive-state-patterns."
category: lwc
salesforce-version: "Spring '25+"
well-architected-pillars:
  - Scalability
  - User Experience
triggers:
  - "lwc shared state"
  - "lightning message service"
  - "pubsub lwc"
  - "lwc global store"
tags:
  - state
  - lms
  - wire
  - architecture
inputs:
  - "components that need to share state"
  - "scope (tab-local / app-wide)"
outputs:
  - "state-management decision + implementation snippets"
dependencies: []
version: 1.1.0
author: Pranav Nagrecha
updated: 2026-08-01
---

# LWC State Management

LWC has four state-sharing mechanisms: (1) parent→child via public @api properties, (2) @wire for server-synced data, (3) Lightning Message Service for sibling LWCs/Aura, (4) custom events for child→parent. This skill picks the right mechanism based on component distance and persistence requirements, and shows why legacy pubsub and heavy external stores (Redux, MobX) should be avoided in favor of these native primitives plus tiny singleton signals.

## Adoption Signals

Any time two non-parent/child LWCs must stay in sync. Not for tiny local state.

- Lightning Message Service (LMS) when sibling components on the same record page must coordinate selection.
- Pubsub when components live across the page hierarchy and an event must traverse Aura/LWC boundaries.

## Recommended Workflow

1. Parent→child: public @api props + custom events.
2. Sibling LWCs: Lightning Message Service via a message channel (`@salesforce/messageChannel/Foo__c`).
3. Server data: `@wire` — it's reactive and cached.
4. App-wide reactive store: a singleton module exporting a signal-like observable; use sparingly.
5. **Platform state managers (Summer '26 GA):** For cross-component reactive state without hand-rolled singletons, use **`defineState()`** from **`@lwc/state`** (`atom`, `computed`, `setAtom` in the setup callback) or the built-in **`lightning/stateManager*`** modules (record, objectInfo, layout, related lists). **Not available in Experience Cloud** at Summer '26 — fall back to LMS or getters there.
6. For Aura↔LWC interop use LMS; pubsub library is legacy.

## Key Considerations

- LMS requires a Custom Metadata record for the channel — check it in.
- @wire refresh via `refreshApex` or `getRecordNotifyChange`.
- Don't import Redux into LWC (bundle bloat); use a tiny hand-rolled store.
- Sibling components must both subscribe after render.

## Worked Examples (see `references/examples.md`)

- *Sibling refresh via LMS* — Edit panel + list panel
- *App-wide current region* — Multi-region switcher

## Common Gotchas (see `references/gotchas.md`)

- **Missing message channel** — LMS silently no-ops.
- **Race on subscription** — First publish missed.
- **Redux-style overhead** — Bundle bloat, debug complexity.

## Top LLM Anti-Patterns (full list in `references/llm-anti-patterns.md`)

- pubsub library in new code
- Bundling Redux/MobX into LWC
- Forgetting to deploy message channel

## Official Sources Used

- Lightning Web Components Developer Guide — https://developer.salesforce.com/docs/platform/lwc/guide/
- Lightning Data Service — https://developer.salesforce.com/docs/platform/lwc/guide/data-wire-service-about.html
- LWC Recipes — https://github.com/trailheadapps/lwc-recipes
- SLDS 2 — https://www.lightningdesignsystem.com/2e/
- State managers GA in LWC (Summer '26 developer blog) — https://developer.salesforce.com/blogs/2026/05/state-managers-are-now-generally-available-in-lightning-web-components

Files in this skill

  • SKILL.md3.6 KB
  • references/examples.md6 KB
  • references/gotchas.md626 B
  • references/llm-anti-patterns.md7.9 KB
  • references/well-architected.md3.1 KB
  • scripts/check_lwc_state_management.py918 B
  • templates/lwc-state-management-template.md542 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…