Sub topic 7
Hook order across parent and child components
This chapter assumes you know what each single-component hook does: constructor(), connectedCallback(), render(), renderedCallback(), and disconnectedCallback(). It also assumes you can nest one component inside another's template. Here you will see how the hooks interleave when components are nested.
Why the order matters
A parent's template contains child components. The parent can't finish rendering until its children exist. That dependency produces a predictable order, and several common bugs come from misreading it:
- Reading a child's DOM from the parent's
connectedCallback()finds nothing. - Expecting a child's data to be ready in the parent's
constructor()fails. - Cleanup that assumes children are already gone runs in the wrong order.
The insertion order
When a parent is inserted into the page, the engine works top-down while creating components and bottom-up while finishing renders.
- The parent's
constructor()runs. - The parent's
connectedCallback()runs. - The parent's template is evaluated, which is its
render()step. This is where the engine discovers its children. - Each child goes through its own
constructor(),connectedCallback(), and render. - Each child's
renderedCallback()runs. - The parent's
renderedCallback()runs last, once all its children have rendered.
If a parent has several children, they are created in the order they appear in the template.
See it in a running example
Log from every hook and watch the console. Create a parent component first.
// parentLogger.js
import { LightningElement } from 'lwc';
export default class ParentLogger extends LightningElement {
constructor() {
super();
console.log('parent constructor');
}
connectedCallback() {
console.log('parent connectedCallback');
}
renderedCallback() {
console.log('parent renderedCallback');
}
}<!-- parentLogger.html -->
<template>
<c-child-logger></c-child-logger>
</template>Then create the child.
// childLogger.js
import { LightningElement } from 'lwc';
export default class ChildLogger extends LightningElement {
constructor() {
super();
console.log('child constructor');
}
connectedCallback() {
console.log('child connectedCallback');
}
renderedCallback() {
console.log('child renderedCallback');
}
}The child needs a template of its own, for example <template><p>Child</p></template>. Placing c-parent-logger on a page prints this:
parent constructor
parent connectedCallback
child constructor
child connectedCallback
child renderedCallback
parent renderedCallbackThe parent's renderedCallback() comes last, even though its constructor() came first.
What this means for your code
| Task | Where it works | Why |
|---|---|---|
| Query a child's DOM from the parent | Parent's `renderedCallback()` | Children have rendered by then. |
| Query a child's DOM from the parent | Not in the parent's `connectedCallback()` | The parent's template has not been rendered, so no children exist. |
| Pass data down to a child | Parent's fields, set before render | The child reads its public properties when it is created. |
| Read a public property inside the child | Child's `connectedCallback()` or later | Properties are not reliable in `constructor()`. |
The removal order
Removal reverses the direction of insertion in one respect. When a parent is removed, its disconnectedCallback() runs first, and then the children's disconnectedCallback() hooks run. Treat the parent's hook as the first one to fire, and don't depend on any other ordering between components. If a child needs to release something, do it in the child's own hook, not from the parent.
Common mistakes with nesting
- Calling a child's method too early. Use
this.template.querySelector('c-child-logger')inrenderedCallback(), not inconnectedCallback(). Before the first render it returnsnull. - Assuming a child renders once per parent render. A child re-renders only when its own reactive state or a public property changes. A parent re-render does not force it.
- Doing heavy work in a parent's
renderedCallback(). It runs after every render, and each child render that completes pushes the parent's hook later. Guard it with a flag, as described in therenderedCallback()chapter.
Exercise
A parent component's template contains one child. You log the name of each hook from both components when the parent is inserted. In what order are parent renderedCallback, child constructor, and parent connectedCallback logged, and in what order do the two disconnectedCallback() hooks run when the parent is later removed?
Show answer
The order is parent connectedCallback, then child constructor, then parent renderedCallback. The parent's renderedCallback() waits for the child to finish rendering. On removal, the parent's disconnectedCallback() runs first and the child's runs after it.
Exercise
You want to focus an input inside a child component when the parent first appears. Why does this.template.querySelector('c-child-logger') return null in the parent's connectedCallback(), and where should the lookup go instead?
Show answer
The parent's connectedCallback() runs before the parent's template is rendered, so the child does not exist yet. Move the lookup into the parent's renderedCallback(), guarded by a flag so it runs only after the first render.