Sub topic 11
Passing Data to a Child with @api Properties
This chapter assumes you can write a component's template and class, bind fields with { }, and write a getter. Here you will pass a value from a parent component into a child component.
Why a child needs @api
A component's fields are private by default. Only its own template and class can read them. To let a parent set a value on a child, the child must mark that field as public with the @api decorator. A decorator is an annotation placed before a field or method that changes how the framework treats it.
Data flows one way: the parent owns the value and passes it down. The child reads it and renders it.
Declare a public property in the child
The child exposes productName with @api. A getter derives the heading from it.
// productCard.js
import { LightningElement, api } from 'lwc';
export default class ProductCard extends LightningElement {
@api productName;
get heading() {
return this.productName ? this.productName.toUpperCase() : 'UNNAMED PRODUCT';
}
}The child's template binds to the getter like any other property.
<!-- productCard.html -->
<template>
<h2>{heading}</h2>
</template>Bind a parent field to the child
The parent sets the child's property in its template. The curly braces point at a property on the parent's own class.
<!-- parent.html -->
<template>
<c-product-card product-name={selectedName}></c-product-card>
</template>The parent's class declares selectedName.
// parent.js
import { LightningElement } from 'lwc';
export default class Parent extends LightningElement {
selectedName = 'Desk lamp';
}When the parent reassigns selectedName, the parent re-renders. The new value reaches the child's productName, and the child re-renders too. Public properties are reactive without any extra decorator.
Naming: camelCase in JavaScript, kebab-case in HTML
HTML attributes are not case-sensitive, so LWC converts names between the two worlds.
| In the child's JavaScript | In the parent's template |
|---|---|
| `productName` | `product-name` |
| `unitPrice` | `unit-price` |
| `quantity` | `quantity` |
The child's tag also changes form. A component in the c namespace named productCard becomes <c-product-card>.
The child must not change what it receives
A public property is read-only from inside the child. If the child assigns to this.productName, the framework logs an error in the console, and the parent's value is not changed. The parent owns the data.
If the child needs a modified version, compute it in a getter, as heading does above. If the child needs to ask the parent for a change, it sends an event. Events are covered in a later part of this tutorial.
Common errors
- Attribute written in camelCase.
productName={selectedName}in the parent's template does not match the child's property. Useproduct-name. - Missing
@api. Without it, the attribute is ignored and the child's field staysundefined. - Reading the property too early. The value is set after the child is created. Do not read
this.productNamein the constructor. Use a getter orconnectedCallback()instead. - Starting a public name with
on. Names such asonSelectcollide with event handler syntax. Choose a name likeselectHandlerorselection. - Expecting an expression.
product-name={selectedName.toUpperCase()}is invalid. Templates accept only property references, so compute the value in the parent with a getter.
Exercise
A child component declares @api unitPrice;. The parent template contains <c-price-tag unitPrice={price}></c-price-tag>, and the child always shows undefined. What is wrong, and how do you fix it?
Show answer
The attribute name is written in camelCase. HTML attribute names are lowercase, so LWC expects the kebab-case form of the property. Change it to <c-price-tag unit-price={price}></c-price-tag>.
Exercise
The child's heading getter should show the product name in lowercase, and a developer writes this.productName = this.productName.toLowerCase(); inside a method of the child. Why is this a mistake, and what is the better approach?
Show answer
A public property is read-only from inside the child, so the assignment is rejected with a console error and the parent's value is not changed. Return this.productName.toLowerCase() from a getter instead, and bind the template to that getter.