Sub topic 4
Binding values from the parent's JavaScript
This chapter covers how a parent component passes values from its own JavaScript class to a child's public properties. It assumes you can already do three things:
- Write a Lightning web component with an HTML template and a JavaScript class.
- Use
{propertyName}expressions in a template. - Read modern JavaScript, including classes, modules, arrow functions and spread syntax.
It also assumes you know that a child exposes a property with @api. Earlier chapters cover that. Here the focus is the parent side of the connection.
Static values and dynamic bindings
A parent can set a child's public property in two ways.
- A static value is a literal string written in the attribute:
heading="Write documentation". The child receives the string exactly as written. - A dynamic binding puts a template expression in braces:
heading={nextHeading}. The child receives whatever the parent's class holds under that name.
<!-- 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 reads nextHeading from the parent's class.
Do not wrap a binding in quotes. heading="{nextHeading}" passes the literal text {nextHeading} as a string. It does not read the field. Write heading={nextHeading} instead.
Static values are always strings
Every static attribute value is a string. This matters for any property that is not text:
<!-- Passes the string "5", not the number 5 -->
<c-task-card estimate="5"></c-task-card>If the child needs a number, an object, an array or a boolean, bind a value from the parent's JavaScript. The child can also convert a string itself, which a later chapter on getters and setters covers.
What you can bind to
The expression inside the braces must be a simple reference to a property or getter on the parent class. Two forms are allowed:
- A field:
heading={nextHeading} - A dot path through an object:
heading={task.name}
LWC templates do not evaluate JavaScript expressions. You cannot write {a + b}, {!isDone}, {task.name.toUpperCase()} or a ternary. When you need a computed value, move the computation into a getter on the parent class and bind to the getter.
// taskBoard.js (parent)
import { LightningElement } from 'lwc';
export default class TaskBoard extends LightningElement {
nextHeading = 'Review pull request';
task = { name: 'Deploy release', priority: 'high' };
isDone = false;
get statusLabel() {
return this.isDone ? 'Done' : 'Open';
}
}<!-- taskBoard.html (parent) -->
<template>
<c-task-card
heading={task.name}
priority={task.priority}
status={statusLabel}>
</c-task-card>
</template>The getter statusLabel does the conditional work. The template only names it.
Binding inside a loop
Inside for:each, the loop variable is also available to bindings. Each child gets values from its own item:
<!-- taskBoard.html (parent) -->
<template>
<template for:each={tasks} for:item="task">
<c-task-card
key={task.id}
heading={task.name}
priority={task.priority}>
</c-task-card>
</template>
</template>The key goes on the outermost element in the loop and must be unique and stable within the list. A later chapter covers lists in full. For now, note that {task.name} here refers to the loop variable and not to a field on the class.
How a binding connects to the child
Each binding names two things. The left side is the child's public property, written in kebab-case. The right side is the parent's value. The diagram shows one binding.
The parent's template reads the field and assigns the value to the child's heading property. The child's template then displays it like any other field.
Rules for binding
- The attribute name on the left must match a public property on the child, after the camelCase to kebab-case conversion. A property named
dueDateis set withdue-date. - If the child has no
@apiproperty with that name, the assignment has no effect. The browser may treat it as a plain HTML attribute, and the child's code never sees the value. - The parent can set a property only on a child inside its own template. It cannot reach into a grandchild.
- Binding is a one-way assignment from parent to child. The child must not write back to a value it received. The next chapters cover this.
Debugging a binding that does nothing
When a bound value does not appear in the child, check these in order:
- Is the child property decorated with
@api? - Does the attribute spelling match, for example
due-datefordueDate? - Did you accidentally quote the braces?
- Is the parent field actually set by the time the template renders? A field that is
undefinedpassesundefined, which looks like a missing value. - Does the child's own template reference the property by its JavaScript name, in camelCase, such as
{dueDate}?
The following table summarises the common mistakes and fixes.
| Symptom | Likely cause | Fix |
|---|---|---|
| Child shows the text `{nextHeading}` | Quotes around the braces | Remove the quotes |
| Child shows nothing | Missing `@api`, or misspelled attribute | Add `@api`; use kebab-case |
| Number arrives as a string | Static attribute used | Bind from the parent's JavaScript |
| Template error on `{a + b}` | Expressions are not allowed | Use a getter |
Exercise
A parent has the field dueDate = new Date('2026-12-01'). The child declares @api dueDate;. Write the attribute that passes the value, and explain why due-date="2026-12-01" would be a different thing.
Show answer
Write due-date={dueDate}. The braces bind the parent's Date object, so the child receives a real Date. The attribute due-date="2026-12-01" is a static value, so the child would receive the string "2026-12-01". The child would then need to parse it before using Date methods.
Exercise
A parent template contains <c-task-card heading={task.name.toUpperCase()}></c-task-card> and the build fails. How do you fix it?
Show answer
Templates cannot call methods. Add a getter to the parent class, for example get upperHeading() { return this.task.name.toUpperCase(); }, and write heading={upperHeading}.