Sub topic 1
Parents, children and the c- tag
Lightning web components (LWCs) are built to be nested. A small component does one job, and a larger component assembles several of them into a screen. This chapter covers the vocabulary and mechanics of that nesting. You will learn how a parent places a child in its template, how a folder name becomes a tag name, and what each side can and cannot see of the other. Later chapters build on this to pass data.
Prerequisites
This chapter assumes you can do the following:
- Read HTML, CSS and modern JavaScript, including classes, modules, arrow functions and spread syntax.
- Create a simple Lightning web component with an HTML template and a JavaScript class.
- Use template expressions such as
{propertyName}to show a class field in a template.
You do not need to know about @api, events or slots yet. This chapter only shows how components are placed inside each other.
What parent and child mean
A parent component is a component whose template contains another component's tag. The component named in that tag is the child. The relationship is defined by where the tag appears in markup, not by anything in the JavaScript.
- A component can be a child in one place and a parent in another. A
taskCardcan be a child oftaskBoardand the parent of apriorityBadge. - A component can have many children, and the same child can appear many times in one parent.
- Each child instance is a separate object with its own state. Two
c-task-cardtags create two independent instances.
This pattern is called composition. You build a feature by combining small components, and you do not extend one large component.
Placing a child with the c- tag
To use a child, write its tag in the parent's HTML template. The tag name has two parts joined by a hyphen: the namespace and the component name.
<!-- taskBoard.html (parent) -->
<template>
<h2>Today</h2>
<c-task-card></c-task-card>
</template><!-- taskCard.html (child) -->
<template>
<article>
<h3>Write documentation</h3>
</article>
</template>// taskCard.js (child)
import { LightningElement } from 'lwc';
export default class TaskCard extends LightningElement {}The parent needs no import statement for the child. The tag in the template is enough, and the framework finds the component by name. This is called declarative composition.
How the tag name is built
The tag name comes from the child's folder name. Each uppercase letter becomes a hyphen followed by the lowercase letter, and the namespace goes in front.
| Folder name | Namespace | Tag in markup |
|---|---|---|
| `taskCard` | `c` | `<c-task-card>` |
| `searchBox` | `c` | `<c-search-box>` |
| `priorityBadge` | `c` | `<c-priority-badge>` |
| `button` (base component) | `lightning` | `<lightning-button>` |
| `taskCard` in a managed package | `acme` | `<acme-task-card>` |
c is the default namespace for components in your own org or project. Components from Salesforce's base library use lightning. A component from a managed package uses that package's namespace.
Base components such as lightning-button are used exactly like your own. You write the tag, and the component renders in place.
Folder naming rules
The tag is derived mechanically from the folder name, so the folder name must follow some rules:
- Start with a lowercase letter.
- Use only letters, numbers and underscores.
- Do not use hyphens, spaces, or two underscores in a row, and do not end with an underscore.
- Keep the folder directly under the
lwcfolder. Components cannot be nested inside other component folders.
The JavaScript, HTML and CSS files inside the folder share the folder's name: taskCard.js, taskCard.html and taskCard.css.
Closing tags are required
HTML lets you write some elements as self-closing tags. LWC templates do not allow this for components. Always write a separate closing tag.
<!-- Wrong: self-closing tags are not supported for components -->
<c-task-card />
<c-task-card />
<!-- Correct -->
<c-task-card></c-task-card>
<c-task-card></c-task-card>The wrong version causes a confusing result. The HTML parser treats the first tag as unclosed, so the second tag ends up inside the first. The page may render only one child, or the compile step may report an error. If you see fewer children than you wrote, check for self-closing tags first.
What the parent owns and what it cannot see
A parent's template contains the child's tag, but it does not contain the child's contents. Each component has its own template, and that template is hidden from other components. This isolation is provided by shadow DOM (or a synthetic version of it, depending on the platform).
The practical effects are:
- The parent can reach the child element.
this.template.querySelector('c-task-card')returns the child's host element, so the parent can call its public methods or read its public properties. - The parent cannot reach inside the child.
this.template.querySelector('h3')will not find the<h3>that lives in the child's template. The query only searches elements that the parent's own template created. - The child cannot reach up. A child has no reference to its parent. It cannot call
this.parent, and a query in the child will not find parent elements. - CSS does not cross the boundary. Styles in the parent's CSS file do not apply to elements inside the child's template, and the reverse is also true.
// taskBoard.js (parent)
import { LightningElement } from 'lwc';
export default class TaskBoard extends LightningElement {
renderedCallback() {
// Finds the child's host element, which the parent's template created.
const card = this.template.querySelector('c-task-card');
console.log(card ? 'child found' : 'child not found');
// Returns null: the <h3> belongs to the child's template.
const heading = this.template.querySelector('h3');
console.log(heading); // null
}
}This one-way visibility is the reason the rest of this tutorial exists. Because the parent cannot edit the child's template, and the child cannot see the parent, communication must go through defined channels. These are public properties, public methods, slots and events.
Lifecycle order between parent and child
Parent and child each have their own lifecycle. When a parent first renders, the order is:
Two consequences follow:
- The parent's
connectedCallbackruns before the child exists, so it cannot call anything on the child. - The child finishes rendering before the parent's
renderedCallbackruns. By then, the parent can safely find and use the child.
The child's constructor runs before any values are set from the parent. Do not read incoming data in the constructor. Later chapters show where to read it safely.
Using one child many times
Each tag creates one instance. You can repeat a tag by hand, or generate tags from a list (covered in a later chapter).
<!-- taskBoard.html (parent) -->
<template>
<section>
<c-task-card></c-task-card>
<c-task-card></c-task-card>
<c-task-card></c-task-card>
</section>
</template>This renders three separate cards. If one card changes its own internal state, the other two are unaffected. Instances share only what the code explicitly shares, such as a module-level variable. Avoid that, because it hides dependencies between instances.
Common mistakes with the tag
- Camel-case in markup.
<c-taskCard>does not work. HTML tag names are case-insensitive, so the camelCase form is lost. Use<c-task-card>. - Wrong namespace.
<lightning-task-card>looks for a base component that does not exist. Usec-for your own components. - Folder renamed, tag not updated. Renaming
taskCardtotodoCardchanges the tag toc-todo-card. Search your templates for the old tag. - Self-closing tag. As shown above, always write
<c-task-card></c-task-card>. - Child not deployed. In a Salesforce project, the child's folder must be in the same deployment or already present in the org. A missing child usually shows as a compile error naming the unknown tag.
Summary of the model
- The parent's template names the child with a tag.
- The tag is the namespace, a hyphen, and the kebab-case form of the child's folder name.
- No import is needed in the parent for markup composition.
- Components need an explicit closing tag.
- The parent sees the child's host element but not its template, and the child cannot see the parent.
- The child's lifecycle hooks run inside the parent's render, and the parent's
renderedCallbackruns last.
The next chapter shows the first communication channel: how the child exposes a public property with @api so the parent can set it.
Exercise
A component lives in a folder named orderSummaryRow in your own project. (1) What tag does a parent use for it? (2) A teammate writes <c-order-summary-row /> and sees only one row rendered when three were expected. What is the likely cause and the fix? (3) In the parent's connectedCallback, a call to this.template.querySelector('c-order-summary-row') returns null. Why?
Show answer
(1) <c-order-summary-row></c-order-summary-row>. The default namespace c is followed by the kebab-case form of the folder name. (2) Components cannot be self-closing in LWC templates. The parser treats the unclosed tags as nested, so the later rows end up inside the first one. Fix it by writing an explicit closing tag on each: <c-order-summary-row></c-order-summary-row>. (3) The parent's template has not rendered when connectedCallback runs, so the child does not exist yet. Run the query in renderedCallback or in response to a user action.
Exercise
Parent app contains <c-search-box></c-search-box>. The searchBox template contains an <input>. Will this.template.querySelector('input') in app.js find that input? Explain why or why not.
Show answer
No. It returns null. The <input> is created by the child's template, and a component's this.template.querySelector only searches elements created by its own template. The parent can reach the c-search-box host element, but not the elements inside it. To act on the input, the parent must call a public method that the child exposes.