Sub topic 14
Lifecycle hooks and the custom element callbacks
This chapter assumes you know JavaScript classes, the DOM, and the earlier chapters on custom elements and component bundles. It covers when the browser and the LWC framework call your code, and which of those calls are standard.
The standard callbacks
A custom element class can define these methods, and the browser calls them:
| Callback | When the browser calls it |
|---|---|
| `constructor` | The element is created. |
| `connectedCallback` | The element is inserted into a document. |
| `disconnectedCallback` | The element is removed. |
| `attributeChangedCallback` | An observed attribute changes. |
| `adoptedCallback` | The element moves to another document. |
LWC exposes the first three. It has no attributeChangedCallback. Parents pass data through @api properties instead of observed attributes. LWC adds render, renderedCallback, and errorCallback. These are framework additions, not browser features.
Order of events
The diagram shows one component's life.
With nested components, the parent goes first until rendering. The parent runs constructor, connectedCallback, and render. Each child then does the same. renderedCallback runs child first, then parent.
What belongs in each hook
constructor: Callsuper()first. The element has no attributes or children yet, so don't read them. Initialize plain state here.connectedCallback: Fetch data, subscribe to events, and read public properties. It can run more than once if the element is moved, so make it safe to repeat.renderedCallback: Work with the rendered DOM, such as measuring an element or starting a third-party library.disconnectedCallback: Undo whatconnectedCallbackset up.
// windowWidth.js
import { LightningElement } from 'lwc';
export default class WindowWidth extends LightningElement {
width = window.innerWidth;
handleResize = () => {
this.width = window.innerWidth;
};
connectedCallback() {
window.addEventListener('resize', this.handleResize);
}
disconnectedCallback() {
window.removeEventListener('resize', this.handleResize);
}
}The listener is an arrow-function field. addEventListener and removeEventListener therefore receive the same function reference, and this stays bound to the component.
Common errors
- Changing a field in
renderedCallbackevery time. The change schedules another render, which callsrenderedCallbackagain, and the loop never ends. Guard the change with a flag.
renderedCallback() {
if (this.initialized) {
return;
}
this.initialized = true;
// one-time DOM setup here
}- Reading the template in
connectedCallback. The template hasn't rendered yet, sothis.template.querySelector(...)finds nothing. UserenderedCallback. - Skipping cleanup. A listener on
windowordocumentkeeps the removed component alive and still running. - Setting attributes in the constructor. The standard forbids it, so the element can't be created reliably.
Exercise
A component adds a scroll listener in connectedCallback. Which hook should remove it, and why?
Show answer
disconnectedCallback. The browser calls it when the element leaves the DOM. Without removal, the listener keeps running and keeps the old component in memory. Pass the same function reference to removeEventListener that you passed to addEventListener.