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()andconnectedCallback()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'srenderedCallbackfires 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.
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.
Lifecycle Hooks in Depth Quiz
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.