Sub topic 8
Sharing data across namespaces with public properties and events
This chapter assumes you know basic Lightning web component structure, modern JavaScript (modules, classes), and how Salesforce namespaces and static resources work. It also assumes you have read the earlier chapters on namespace isolation and DOM access.
Why the boundary matters
Lightning Web Security (LWS) runs each namespace in its own JavaScript sandbox. Code in one namespace gets its own view of globals such as window and document. A component from namespace acme cannot read or change objects that a component from namespace c created. It also cannot walk into that component's DOM.
Components in different namespaces still need to work together. A parent in c may embed a managed-package child from acme. LWS supports this through two public contracts:
- Public properties, which pass data down from parent to child.
- Custom events, which pass data up from child to parent.
Everything else, such as shared global variables or direct DOM reach-in, is blocked or distorted.
Passing data down with @api
A child exposes a property to its parent with the @api decorator. The parent sets it in its template, and LWC delivers the value across the boundary.
// child component (any namespace)
import { LightningElement, api } from 'lwc';
export default class ItemPicker extends LightningElement {
@api label;
handleSelect() {
this.dispatchEvent(
new CustomEvent('select', { detail: { id: '001' } })
);
}
}The parent template sets the property like an HTML attribute:
<template>
<acme-item-picker label="Choose an item" onselect={handleSelect}></acme-item-picker>
</template>The label is a string, so it crosses without trouble. Objects and arrays also cross, but the receiving side should treat them as read-only. A child must not mutate an object that its parent passed in. The platform may hand the child a read-only proxy of the parent's data, and writes to it throw an error. Copy the value first if you need to change it.
Passing data up with custom events
The child's handleSelect method dispatches a select event with a detail payload. The parent listens with onselect and reads event.detail.
// parent component in namespace c
import { LightningElement } from 'lwc';
export default class Parent extends LightningElement {
selectedId;
handleSelect(event) {
this.selectedId = event.detail.id;
}
}Keep detail simple. Send plain values, plain objects, and arrays of those. Do not send functions, class instances, DOM nodes, or objects that belong to another sandbox. These can be distorted or blocked at the boundary, and they tie your components together in ways that break when either side changes.
Choosing what to share
| Need | Use | Avoid |
|---|---|---|
| Parent configures child | `@api` property | Querying into the child's DOM |
| Child reports a result | `CustomEvent` with a small `detail` | Writing to `window` for the parent to read |
| Child exposes an action | `@api` method | Calling private methods or reaching internals |
| Unrelated components talk | Lightning Message Service | Shared globals |
An @api method works like a property for the parent to call. For example, a parent can call this.template.querySelector('acme-item-picker').reset(). The parent queries its own template and then uses only what the child publishes.
Common mistakes
- Setting
window.sharedState = {...}in one component and reading it in another. Each namespace sees its ownwindow, so the second component getsundefined. - Mutating an
@apiobject inside the child. Clone it first with{ ...this.config }orstructuredClone. - Putting a DOM element in
event.detail. The receiver gets a distorted reference at best. - Setting
bubbles: trueandcomposed: truewithout need. Composed events cross shadow boundaries, so any component above can listen. Keep events narrow unless several levels must hear them.
Exercise
Exercise
A component in namespace c stores a result in window.lastResult. A child component from namespace acme reads window.lastResult and gets undefined. Why does this happen, and what should you use instead?
Show answer
Under LWS each namespace runs in its own sandbox with its own window, so the value set in c is not visible to acme. Pass the value to the child through an @api property, or have the child send data to the parent with a CustomEvent whose detail holds plain data.