Sub topic 1
The problem: components with no shared parent
Lightning web components on a page often need to share information, but they don't always sit in the same component tree. This chapter explains why the standard communication tools stop working in that case. It also sets up the problem that Lightning Message Service solves.
Prerequisites
This chapter assumes you already know:
- Basic Lightning Web Components: templates, JavaScript classes, and decorators such as
@wire. - Component lifecycle hooks such as
connectedCallbackanddisconnectedCallback. - How a Salesforce DX project is laid out and how to deploy metadata.
- How to place components on a Lightning page with the App Builder.
A common scenario
Imagine a Lightning record page with two components. A recordList component on the left shows a list of records. A recordDetail component on the right shows details for the record the user selects.
An admin drags both components onto the page in App Builder. Neither one contains the other. Their parent is the page itself, and you cannot write code in that parent. Yet when the user clicks a row in recordList, recordDetail must update.
What standard LWC communication gives you
Lightning Web Components provide two main ways to pass data. Both depend on a parent-child relationship.
- Public properties (marked
@api) pass data down. A parent sets a property on a child in its template. - Custom events pass data up. A child dispatches an event with
dispatchEvent, and a parent listens for it.
A parent can also call a public method on a child. All of these assume that one component renders the other, or that one is an ancestor of the other.
Why these tools fail for unrelated components
Look at each tool against the scenario.
| Tool | Direction | Why it fails here |
|---|---|---|
| Public property | Parent to child | `recordDetail` is not a child of `recordList`, and no parent template exists to set the property. |
| Custom event | Child to parent | Events travel up the DOM tree. The event from `recordList` never passes through `recordDetail`. |
| Public method | Parent to child | You need a reference to the other component, and neither one has it. |
A custom event can be made to bubble and compose through shadow boundaries. That only helps ancestors in the same tree. Two sibling components on a page, each placed separately, are not ancestors of each other. The event cannot reach a component on a different branch.
// recordList.js
// This event goes up the DOM from recordList.
// recordDetail is on a different branch, so it never sees it.
handleSelect(event) {
this.dispatchEvent(
new CustomEvent('recordselected', {
detail: { recordId: event.currentTarget.dataset.id },
bubbles: true,
composed: true
})
);
}Nothing is wrong with this code. It is the wrong tool for the layout.
Workarounds that cause trouble
Developers who hit this problem often try one of the following. Each one has a cost.
- Wrap both components in a new parent. This works when you control the layout. It does not work when an admin places the components separately, and it ties the two components together.
- Use
document.querySelectorto find the other component. Lightning Web Security and Lightning Locker restrict access across component boundaries, and the code breaks easily. - Write to a shared global variable. Nothing tells the other component that the value changed, and the approach is fragile.
- Poll a server record. This adds server calls and delay for something that happens entirely in the browser.
The shape of the solution
The two components need a way to talk that does not depend on where they sit. Think of a radio channel. The publisher broadcasts on a named channel. Any subscriber tuned to that channel hears the message. Neither side knows about the other.
That is the model Lightning Message Service uses. Components share the name of a channel, not a reference to each other.
The later chapters build this out. A message channel acts as the shared contract. A publisher sends a message on it, and subscribers receive it.
Key points
- Public properties and custom events need a parent-child relationship.
- Components placed separately on a Lightning page are not related in that way.
- Workarounds such as DOM queries or shared globals are fragile.
- A named channel that both sides know removes the need for a direct link.
Exercise
A Lightning page has component A and component B, placed side by side by an admin. A dispatches a custom event with bubbles and composed both set to true. Why does B not receive it, and what kind of solution fits this layout?
Show answer
Events travel up through a component's ancestors. B is on a different branch and is not an ancestor of A, so the event never reaches it. Setting bubbles and composed only affects how far the event travels up the tree, not sideways. The layout needs a publish-subscribe approach, such as a named message channel, that does not depend on the component hierarchy.
Exercise
Name two reasons why wrapping both components in a new parent component is not always a good fix.
Show answer
It only works when the developer controls the layout, so it fails when an admin places the components separately in App Builder. It also couples the two components to a shared parent, which reduces how reusable each one is.