Sub topic 4
Binding values from the parent's JavaScript
This chapter assumes you can build a simple Lightning web component and that you know @api makes a child field public. Here you connect a parent's JavaScript values to those child properties.
Static and dynamic values
A parent can set a child's public property in two ways. A literal value in quotes is fixed in the markup. A value in curly braces is read from the parent's JavaScript class.
<!-- taskBoard.html (parent) -->
<template>
<c-task-card heading="Write documentation" priority="high"></c-task-card>
<c-task-card heading={nextHeading}></c-task-card>
</template>The first tag passes two literal strings. The second tag passes whatever nextHeading holds in the parent class.
// taskBoard.js (parent)
import { LightningElement } from 'lwc';
export default class TaskBoard extends LightningElement {
nextHeading = 'Review pull request';
}The child declares heading and priority with @api, as in the previous chapter. It doesn't know whether a value was literal or bound.
Rules for the braces
- Write
heading={nextHeading}with no quotes around the braces. Quotes turn the value into a literal string. - The name inside the braces must be a property or getter on the parent class. It can't be a local variable or a method call.
- LWC templates don't allow expressions.
{first + last}and{count > 0}don't work. Compute the value in JavaScript and bind to the result.
Computed values with getters
When the child needs a derived value, add a getter to the parent and bind to it.
// taskBoard.js (parent)
import { LightningElement } from 'lwc';
export default class TaskBoard extends LightningElement {
firstName = 'Ada';
lastName = 'Lovelace';
get ownerName() {
return `${this.firstName} ${this.lastName}`;
}
}<!-- taskBoard.html (parent) -->
<template>
<c-task-card heading={ownerName}></c-task-card>
</template>The getter runs again whenever a field it reads changes. The new result then reaches the child. The next chapter describes that update path in detail.
Mixing static and bound values
Both styles can appear on the same child tag. Use a literal when the value never changes, such as a fixed label. Use a binding when the value depends on state, user input or data loaded by the parent.
| Style | Markup | Value comes from |
|---|---|---|
| Static | `priority="high"` | The template text |
| Dynamic | `priority={level}` | A field or getter on the parent class |
What the child receives
The child gets the value as a property on its own class, so it reads this.heading in JavaScript and {heading} in its template. If the parent binds a name that doesn't exist, the child receives undefined. The page doesn't throw an error, so a missing heading usually means a typo in the bound name.
Exercise
A parent has fullTitle as a getter that returns ${this.prefix} ${this.name}. Write the markup that passes it to c-task-card as heading. Then explain why heading={prefix + name} would not work.
Show answer
Use <c-task-card heading={fullTitle}></c-task-card>. LWC templates accept only a property or getter name inside the braces, not an expression, so prefix + name is invalid. Compute the combined value in a parent getter and bind to that getter.