Sub topic 7
Exposing public properties with @api
This chapter assumes you can write a basic Lightning web component (LWC), know that class fields are reactive, and understand the difference between reassigning a field and mutating an object. It covers how a parent component passes data into a child through public properties.
What @api does
By default, a field in a component class is private. Only that component's own class and template can read it. A parent cannot set it from its template.
The @api decorator makes a field public. A public property is part of the component's contract with the outside world. A parent can set it, and the child can read it in its own template. Public properties are also reactive. When the parent assigns a new value, the child rerenders if its template uses that property.
import { LightningElement, api } from 'lwc';
export default class InfoCard extends LightningElement {
@api heading;
@api expanded = false;
}Here heading has no default value, so it is undefined until a parent sets it. expanded starts as false.
Setting public properties from a parent
A parent sets a child's public properties as attributes in its template. HTML attribute names are lowercase, so a camelCase property name is written in kebab-case. The component name follows the same rule: InfoCard in the c namespace becomes c-info-card.
<template>
<c-info-card heading="Release notes" expanded></c-info-card>
</template>Two details to notice:
heading="Release notes"sets a string.- A bare
expandedattribute sets the property totrue. This works for boolean properties that default tofalse.
To pass a value from a parent field instead of a literal, use curly braces, such as heading={title}. The parent's field is then the source of truth. When the parent reassigns title, the child receives the new value and rerenders.
Why the child should treat @api properties as read-only
Data in LWC flows one way, from parent to child. The parent owns the value. The child receives it and displays it. If the child assigns to its own @api property, the parent's value and the child's value can disagree, and the parent will overwrite the child's change the next time it passes a value.
The framework enforces part of this. When a parent passes an object to a child, the child gets a read-only view of it, so changing a property inside that object throws an error. Reassigning a primitive @api field inside the child does not always throw, but it is still a mistake. It breaks the ownership rule.
If the child needs a different value, it has two options:
- Copy the value into a private field and change the copy.
- Dispatch an event so the parent can decide whether to change its own data. Events are covered in a later chapter.
import { LightningElement, api } from 'lwc';
export default class EditableTitle extends LightningElement {
@api heading;
draft;
connectedCallback() {
this.draft = this.heading;
}
handleInput(event) {
this.draft = event.target.value;
}
}In this example heading is never modified. The component edits draft, which is private.
Common mistakes with public properties
- Using camelCase in the attribute. Writing
<c-info-card isExpanded>does not work. Writeis-expanded. - Starting a name with
dataoraria, or using reserved names. Names likeclass,slot, andstyleconflict with standard HTML attributes. Avoid them for@apiproperties. - Expecting a value in the constructor. Parents set public properties after the child is created. Read them in
connectedCallbackor later. - Defaulting a public boolean to
true. A bare attribute can only settrue, so a parent cannot turn such a property off with the attribute alone. Default public booleans tofalse.
Check your understanding
Exercise
A parent template contains <c-info-card heading={title}></c-info-card>. The parent later runs this.title = 'Updated'. What happens in the child, and why should the child not assign to this.heading itself?
Show answer
The child's heading property receives 'Updated' and the child rerenders if its template uses heading, because @api properties are reactive. The child should not assign to this.heading because the parent owns that value. A child assignment would put the two components out of sync, and the parent would overwrite it on its next update. The child should copy the value into a private field or dispatch an event.
Exercise
Write the attribute a parent uses to set a child's public property isExpanded to true.
Show answer
is-expanded, written as <c-info-card is-expanded></c-info-card>. The camelCase property name becomes kebab-case in the template, and a bare attribute sets a boolean property to true.