Sub topic 1
Parents, children and the c- tag
Prerequisites
This chapter assumes you can do the following:
- Read basic HTML, CSS and modern JavaScript (classes, modules, arrow functions, spread syntax).
- Create a simple Lightning web component with an HTML template and a JavaScript class.
- Use template expressions such as
{propertyName}in a component template.
Composition: one component inside another
Lightning web components are built by nesting. A component places another component in its template, the same way it places a <div> or a <p>. The component that contains the other is the parent. The component that is contained is the child.
Composition lets you split a screen into small pieces that each do one job. A task board can delegate the display of each task to a task card component. Later chapters show how data moves between the two. This chapter covers how the child gets into the parent's template.
Naming the child in markup
You don't import a child in JavaScript to use it in a template. You write a tag whose name is built from the child's namespace and folder name.
- Start with the namespace. In most projects it is
c, the default namespace. - Add a hyphen.
- Add the component's folder name, converted from camelCase to kebab-case.
| Component folder | Namespace | Tag in markup |
|---|---|---|
| `taskCard` | `c` | `c-task-card` |
| `searchBox` | `c` | `c-search-box` |
| `taskList` | `c` | `c-task-list` |
Here is a minimal child and a parent that contains it.
<!-- taskCard.html (child) -->
<template>
<article>
<p>A single task</p>
</article>
</template>// taskCard.js (child)
import { LightningElement } from 'lwc';
export default class TaskCard extends LightningElement {}<!-- taskBoard.html (parent) -->
<template>
<h2>Today</h2>
<c-task-card></c-task-card>
<c-task-card></c-task-card>
</template>The parent renders two independent instances of TaskCard. Each instance has its own state.
What "parent" and "child" mean
The relationship is defined by whose template contains the tag. If taskBoard.html contains <c-task-card>, then taskBoard owns that card. The owner can set the child's public properties and call its public methods. A child has no direct access to its parent. It can only communicate upward by dispatching events, which a later chapter covers.
The same component can be a parent and a child at once. taskBoard is the parent of taskCard, and taskBoard can itself be the child of an app component.
Rules for the tag
The tag follows HTML parsing rules plus a few LWC rules.
- Always write a closing tag.
<c-task-card></c-task-card>works.<c-task-card />does not, because HTML ignores the self-closing slash on custom elements and the following markup ends up inside the child. - Use lowercase kebab-case.
<c-taskCard>and<c-TaskCard>don't match any component. - Match the folder name exactly. A folder named
taskcardgivesc-taskcard, notc-task-card. - Use the right namespace. A component from a managed package or another namespace uses that namespace in place of
c.
Troubleshooting a child that does not appear
If the child doesn't render, check these causes in order:
- Compare the tag with the folder name, letter by letter.
- Confirm the child's folder contains a JavaScript file with a default export that extends
LightningElement. - Look for a self-closing tag and replace it with an opening and closing pair.
- Check that the child is deployed to the same org or project as the parent.
Exercise
A component lives in a folder named orderSummary in the default namespace. What tag do you write in a parent template, and why does <c-order-summary /> cause trouble?
Show answer
Write <c-order-summary></c-order-summary>. The browser ignores the self-closing slash on custom elements, so the element stays open and any markup after it becomes part of the child. Use an explicit closing tag.