Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 38 ยท Events and Slots

Component Communication Patterns in Lit

Learn how Lit components talk to each other: properties down, events up, shared stores with controllers, mediator components and when to avoid reaching into children.

A page of web components is a tree. Parents contain children, siblings sit side by side, and distant relatives sometimes need to share data. How these parts talk to each other decides whether an app stays understandable at 10 components or collapses at 100.

Lit does not impose a state-management system, which is a feature: the platform already gives you a clean default. This lesson names the patterns, shows each one running, and tells you when to move from one to the next.

The default: properties down, events up

The simplest rule: a parent passes data to children through properties; a child tells its parent what happened through events. The child never reaches up to change the parent, and the parent never digs into the child's shadow DOM.

Properties down, events up

Parent to child .value=${data}
Child to parent dispatchEvent(...)
Never child edits parent state directly

Data flows one way; changes are announced.

1 / 2
A parent with two children

<shopping-cart> owns count. qty-input and cart-total are "dumb": they display what they are given. Because both read from the same parent state, they can never disagree. Keep the owner as low in the tree as possible, but high enough to be a common ancestor of every component that needs the data.

Calling methods on children

Sometimes a parent must tell a child to do something: dialog.show(), input.focus(), player.play(). Properties describe state, but commands are not state, so exposing a public method is the cleanest option.

Public methods

Keep public methods few and name them like verbs. A property such as open can sit next to show()/hide(): the method is a convenience, and the property keeps the state declarative.

Siblings talk through the parent

Two siblings should not hold references to each other. The standard route is: sibling A emits an event, the shared parent updates state, and sibling B receives the new property. The shopping cart above is exactly that pattern.

If the siblings are far apart (different branches of the page), the parent chain becomes long. You then have three options, covered below: a shared store, the context protocol, or a window-level event bus.

A shared store with a reactive controller

A store is a plain object (or class) that holds state and notifies subscribers. Combined with a reactive controller, every component that uses the store re-renders automatically when it changes, with no parent in between.

Shared store

The two components share no parent and never reference each other. Production stores add features (derived values, persistence, devtools), and libraries such as signals, Redux or Zustand all plug in the same way: subscribe in hostConnected, call requestUpdate on change, unsubscribe in hostDisconnected.

A window-level event bus

An event bus decouples senders and receivers completely: anyone dispatches on a shared target, anyone listens. It is easy to start with and hard to maintain, because you cannot see who reacts to what.

JavaScript
// sender
window.dispatchEvent(new CustomEvent("app:toast", { detail: "Saved" }));

// receiver (in connectedCallback / disconnectedCallback)
window.addEventListener("app:toast", this._onToast);

Prefix event names (app:) to avoid collisions, keep payloads small, and prefer a store for anything stateful. Use a bus for fire-and-forget notifications such as toasts and analytics only.

Choosing a pattern

Situation Pattern
Parent configures a child Properties (.prop=${...})
Child reports a user action Custom event on the child
Parent commands a child Public method
Two siblings share data Lift the state to the parent
Many distant components share data Store + controller, or context
Fire-and-forget app-wide notices Event bus on window

Anti-patterns to avoid

  • Reaching into a child's shadow root (child.shadowRoot.querySelector(...)) from the parent. Expose a property or method instead.
  • Children mutating a parent's data through a passed-in object. Emit an event and let the owner decide.
  • Everything in a global store. Local UI state (an open dropdown) belongs in the component.
  • Event chains with no owner, where A fires an event that B converts into another event for C. Hoist the state instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the properties-down, events-up pattern, how does a child component tell its parent that the user picked an item?

    Children announce changes through events; the owner of the state decides what to do.
  2. 2. Two sibling components need the same data. What is the simplest correct design?

    A single owner keeps both views consistent.
  3. 3. Which tool should a component use to re-render when an external store changes?

    A controller ties the subscription to the component's lifecycle and triggers a normal update.
  4. 4. When is a public method on a component the right way for a parent to talk to it?

    Commands are actions, not state, so methods suit them better than properties.

Summary

  • Default to properties down and events up; keep state in the lowest common ancestor.
  • Use public methods for commands, and never reach into another component's shadow DOM.
  • A store plus a reactive controller shares state across distant components.
  • Use a window event bus only for fire-and-forget notifications.

Next: The Context Protocol