Advanced 20 min read

Lifecycle Hooks in Depth

By the end of this lesson, you'll be able to:

  • Recall the full sequence of LWC lifecycle hooks and their firing order
  • Understand the parent/child ordering differences between hooks
  • Use errorCallback to catch errors from child components

Prerequisites: Module 10: "Project: Account Management Dashboard"

The Full Lifecycle Sequence

Module 3's "The Component Lifecycle" lesson introduced the basic hooks; this lesson covers the ordering in depth, which matters once components nest.

  • constructor() and connectedCallback() fire top-down — parent before child. A parent begins constructing/connecting before its children do, since the parent has to exist first to contain them.
  • renderedCallback() fires bottom-up — child before parent. Children must finish rendering before their parent can be considered fully rendered, so the parent's renderedCallback fires last.
  • disconnectedCallback() fires top-down, same as connection — parent first, then children, as the tree is torn down.

Why the Order Matters

A common bug: reading a child's rendered content from a parent's renderedCallback too early, assuming parent-then-child order everywhere — but rendering is bottom-up, so by the time the parent's renderedCallback fires, every child has already finished. Getting this backwards (assuming top-down for renderedCallback) leads to reading stale or incomplete child state.

errorCallback

export default class DashboardContainer extends LightningElement {
    hasError = false;

    errorCallback(error, stack) {
        this.hasError = true;
        console.error('A child component failed:', error, stack);
    }
}

errorCallback(error, stack) is a special hook available on a parent component — it catches errors thrown during a child's lifecycle hooks or rendering, letting the parent recover gracefully (e.g. showing a fallback message) instead of the whole component tree breaking.

Exercise

A parent component needs to read a value from a child's rendered DOM. Which hook should this code run in, and why?

Show hint

Think about which hook guarantees children have finished rendering.

JAVASCRIPT

Exercise

Challenge: explain, as a comment, what errorCallback is for and where it must be defined to catch a child's error.

Show hint

Recall which component in the tree needs the hook.

JAVASCRIPT

Lifecycle Hooks in Depth Quiz

1. In what order does connectedCallback fire across a parent and its children?

2. In what order does renderedCallback fire across a parent and its children?

3. What does errorCallback let a parent component do?

4. Why does renderedCallback fire bottom-up rather than top-down?

5. Does a component's own errorCallback catch errors thrown by that same component itself?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Module 3 gave a light introduction to the component lifecycle; this is the promised full depth — the exact firing order, why it differs between hooks, and errorCallback as a way to catch child component failures.