Pass a set of properties in an object to a child component using the lwc:spread directive. lwc:spread also enables elements to accept an object that’s bound as properties at runtime.
You don’t have to manually define each attribute or property in your template like this.
1<!-- Replace this with lwc:spread -->2<c-mycomponent name={config.name} country={config.country}></c-mycomponent>
lwc:spread makes it easier to work with large config or objects. You can dynamically pass multiple attributes or properties. However, it only spreads top-level properties. Nested objects won’t be automatically spread.
The lwc-recipes repo has an apiSpread component that demonstrates the lwc:spread directive.
lwc:spread is always applied last, so it overrides any properties that are directly declared in the template. Only one lwc:spread instance can be used on a directive.
In this example, c-child passes name as Lightning Web Components even though the parent component is passing in name="lwc". Ultimately, the child component takes the "Lightning Web Components" name.
1// app.js2import{LightningElement}from "lwc";34export default class extends LightningElement{5 childProps = {name: "Lightning Web Components"};6}
Use lwc:spread With an Event Handler
lwc:spread doesn’t bind the component to the event handlers defined in the template. For example, you can pass in an onclick handler in the object as the property name.
This release is in preview. Features described here don't become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can't guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.