Overview
ThecompositionBasics component demonstrates the fundamental pattern of component composition in Lightning Web Components. It shows how to:
- Nest a child component within a parent component
- Pass data to child components using public
@apiproperties - Create reusable component hierarchies
Parent Component Structure
The parent component (compositionBasics) defines contact data and passes it to the child component.
JavaScript (compositionBasics.js)
Template (compositionBasics.html)
Child Component Structure
ThecontactTile component receives data through its @api property and renders the contact information.
JavaScript (contactTile.js)
Template (contactTile.html)
Key Concepts
@api Decorator
The@api decorator makes a property public, allowing parent components to set its value:
Data Binding
In the parent template, use curly braces to bind data to the child component’s property:Component Naming
Child components are referenced using kebab-case with thec- namespace:
- Component name:
contactTile - Template usage:
<c-contact-tile>
Usage Pattern
This pattern is ideal for:- Creating reusable UI components
- Separating concerns between parent and child components
- Building component libraries with consistent interfaces
- Maintaining clean, modular code
Source Files
- Parent:
force-app/main/default/lwc/compositionBasics/ - Child:
force-app/main/default/lwc/contactTile/
