Understanding Lifecycle Hooks in Lightning Web Components (LWC) A Developer's Guide

Understanding Lifecycle Hooks in Lightning Web Components (LWC): A Developer’s Guide

Lifecycle hooks in Lightning Web Components (LWC) are critical for controlling component behavior during creation, rendering, and destruction. Understanding these hooks ensures efficient resource management, optimal performance, and robust error handling. In this post, we’ll explore each lifecycle hook, provide real-world examples, and discuss their pros, cons, and use cases.

Table Contents –

Before going in details, lets’ understand what is host element.

What is a Host Element?

You can access it via this, this.template and this.template.host in your JavaScript, as below –

1. this Refers to the component instance (JavaScript class). Can be used to access reactive properties, methods, or @api public properties/methods defined in the component’s class.

Example:

export default class MyComponent extends LightningElement {
  myProperty = 'value';
  
  handleClick() {
    console.log(this.myProperty); // Access component's property
  }
}

2. this.template – Refers to the The component’s shadow root, representing the DOM inside the component’s template.. Used to query or manipulate elements within the component’s shadow DOM.

Example:

<!-- Component Template -->
<template>
  <div data-id="myDiv">Hello</div>
</template>
// Access the div element
renderedCallback() {
  const div = this.template.querySelector('[data-id="myDiv"]');
}

3. this.template.host – Refers to The host DOM element (the custom element itself, e.g., <my-component>). Used to interact with the host element’s attributes e.g., adding a class or retrieving an attribute).

Example:

connectedCallback() {
  // Add a class to the host element
  this.template.host.classList.add('my-class');
  
  // Get an attribute from the host
  const id = this.template.host.getAttribute('data-id');
}

Types of Life cycles Hooks –

Below are the 5 lifecycle hooks that we have in LWC in Salesforce –

  1. constructor()
  2. connectedCallback()
  3. renderedCallback()
  4. disconnectedCallback()
  5. errorCallback(error, stack)

Let’s see them in details –

1. constructor()

  • Key Points:
    1. Flows from parent to child i.e. if you have a parent and child component, the constructor() defined in the parent component will fire first.
    2. Used for initializing private variables.
    3. The first statement must be super(). This is because the Lightning Web Component inherits from LightningElement. It includes its own constructor that should not be skipped during initialization.
    4. Child elements and Public properties (@api) can’t be accessed. This is because they don’t exist yet and are undefined.
    5. You cannot access or interact with host element and also can’t add properties to host element. This is because the component is not fully loaded in the DOM.
    6. Avoid return (other than an early return;) or using document.write()/open().
    7. Reference Link for for some more constructor() use case examples – Link
Constructor example Code –

The example LWC showcases the below functionality –

  1. Initialization of Private Properties
    • In the constructor, private properties are initialized with default values. For instance:
      • count is initialized to 0.
      • isLoading is initialized to false.
      • These initializations ensure the component has well-defined states before interacting with other parts of the application.
  2. Inaccessibility of Public Properties (@api) in Constructor
    • Public properties, such as message, cannot be accessed in the constructor and return undefined. This limitation exists because public properties are only available after the component’s lifecycle progresses beyond the constructor.
  3. Inaccessibility of Child Elements in Constructor
    • Attempting to access child elements (e.g., <lightning-button>) using this.template.querySelector in the constructor results in null. This happens because the DOM is not yet rendered during the constructor phase.
  4. Avoiding DOM Manipulation of Host Elements in Constructor
    • Manipulating the DOM or adding attributes to the host element in the constructor is not recommended. For example:
      • Adding a CSS class like child-host to the host element in the constructor is discouraged. This is because the host element does not exist during this phase.
<!–constructorEx.html–>
<template>
<lightning-card title="Constructor Example – LWC">
<div style="padding: 1rem;">
<lightning-button label="Sample Button"></lightning-button>
</div>
</lightning-card>
</template>
/*constructorEx.js*/
// Import necessary modules from the LWC framework
import { LightningElement, api } from "lwc";
// Export the TestLWC class
export default class ConstructorEx extends LightningElement {
// Public property exposed via @api decorator
@api message;
// Internal properties
count;
isLoading;
/**
* Lifecycle hook invoked when the component instance is created.
* Used for initial setup, such as initializing private properties.
* Note: Public properties and the DOM are not accessible in the constructor.
*/
constructor() {
// Mandatory call to the parent class constructor
super();
// Initialize private property `count` to 0
this.count = 0;
console.log("Constructor: count =", this.count); // Logs 0
// Initialize private property `isLoading` to false
this.isLoading = false;
console.log("Constructor: isLoading =", this.isLoading); // Logs false
// Attempting to access public property `message` is not allowed at this stage
console.log("Constructor: message =", this.message); // Logs undefined
// Attempting to access DOM elements is not possible in the constructor
console.log(
"Constructor: button =",this.template.querySelector("lightning-button")
); // Logs null, as DOM elements are not yet available
// Modifying the host element in the constructor is also not allowed
// Instead do it in connected callback, the below will give error in constructor.
/*this.host.classList.add('child-host');*/
}
}
ProsCons
Allows for early initialization of private variables.Cannot access child elements or inspect attributes as they are not yet available.
Ensures prototype chain is correctly set up using super().Cannot access public properties (@api) as they are assigned after the constructor.
Enables lightweight setup tasks without rendering overhead.Risk of misusing it for DOM-related tasks, leading to errors.
Guarantees a predictable flow from parent to child components.Limited functionality compared to other hooks (e.g., no DOM interaction).

2. connectedCallback()

  • Key Points:
    1. Flows from parent to child i.e. if you have a parent and child component, the connectedCallback() defined in the parent component will fire first.
    2. To check if a component is connected to the DOM use this.isConnected.
    3. Can be used for –
    4. Public properties (@api) can be accessed.
    5. You can access or interact with host element and also add properties to host element, using this, this.template and this.template.host.
    6. Child elements can’t be accessed. This is because they don’t exist yet and are undefined.
    7. Can run multiple times, such as when an element is reordered. Add logic to ensure code runs only once if needed.
    8. Reference Link for for some more connectedCallback() use case examples – Link
Connected Call back example Code –

The example LWC showcases the below functionality –

  • Child Component:
    1. Fetching account data using Apex
      • The child component fetches account data. It does this by invoking the fetchAccounts method from the AccountController Apex class.The results are assigned to the accounts property, which dynamically updates the UI to display account names. Any errors during the data fetch are handled and logged.
    2. Assigning a click event listener to the host element
      • In the connectedCallback method, the child component adds a “click” event listener to its host element using the addEventListener method. This listener is defined as the handleHostClick method. It dispatches a custom event called hostclick whenever the host element is clicked.
    3. Setting attributes like CSS classes and custom attributes to the host element
      • The child component modifies the host element by:
        • Adding a CSS class child-host to apply specific styles.
        • Setting a custom data attribute “custom_attr” with the value “Data from child”.
        • These modifications allow the parent component to apply and detect specific styles or values.
    4. Demonstrating that child elements (like the <h1> element) cannot be accessed
      • Attempts to access the <h1> element within the child component during the connectedCallback lifecycle hook result in undefined. This is because they don’t exist yet in the DOM.
    5. Demonstrating that public properties (valuePassedFromParentUsingProperty) can be accessed
      • Trying to access the public property valuePassedFromParentUsingProperty results in values. This is because all the public properties (decorated with @api) are received from the parent component.
  • Parent Component:
    1. The parent component includes a element and binds a hostclick event handler (handleHostClick). When the child’s host element is clicked, the parent component updates the parentValue property. It updates this property with data from the child element’s custom attribute (custom_attr).
When the Child Component loads, custom styles are applied. The account data is loaded via an Apex method. Location details are fetched through an external API callout.
After clicking on child component, event is passed to parent component, and parent gets custom_attr value in Parent Value.
/*child.css*/
/*applying style to host directly*/
:host {
display: block;
padding: 1rem;
margin: 1rem;
transition: all 0.3s;
}
/*applying style to host using a class*/
:host(.child-host) {
border: 2px solid red;
}
/*applying style to host using a custom_attr*/
:host([data-custom_attr="Data from child"]) {
box-shadow: 0 2px 8px greenyellow;
}
/*applying style to host based on class property passed from parent*/
:host(.parent-supplied-class) {
border-radius: 8px;
background-color: skyblue;
}
.content {
padding: 1rem;
}
view raw child.css hosted with ❤ by GitHub
<!–child.html–>
<template>
<div class="content">
<div class="test">
<p><b>Child Component</b></p>
</div><br/>
<div class="test">
<lightning-button label="Sample Button"></lightning-button>
</div>
<br />
<!–Account Location Details –>
<div class="slds-var-p-vertical_xxx-small slds-text-heading_small"><b>Fetching Account Details Using Apex</b></div>
<template if:true={accounts}>
<ul>
<template for:each={accounts} for:item="account">
<li key={account.Id}>{account.Name}</li>
</template>
</ul>
</template>
<template if:true={error}>
<p>Error: {error.body.message}</p>
</template>
<br />
<!–Display Location Details –>
<div class="slds-var-p-vertical_xxx-small slds-text-heading_small"><b>Fetching Location Details Using External Api Call Out</b></div>
<div if:true={locationPopulated}>
<div class="slds-var-p-vertical_xxx-small slds-text-heading_xx_small"><b>Country Code : </b> {locationInfo.country_code}</a></div>
<div class="slds-var-p-vertical_xxx-small slds-text-heading_xx_small"><b>Timezone : </b>{locationInfo.timezone_id}</a></div>
<div class="slds-var-p-vertical_xxx-small slds-text-heading_xx_small"><b>Latitude : </b> {locationInfo.latitude}</div>
<div class="slds-var-p-vertical_xxx-small slds-text-heading_xx_small"><b>Longitude : </b> {locationInfo.longitude}</div>
<div class="slds-var-p-vertical_xxx-small slds-text-heading_xx_small"><b>Map Url : </b>
<a href={locationInfo.map_url} target="_blank" rel="noopener noreferrer">Link</a>
</div>
</div>
</div>
</template>
view raw child.html hosted with ❤ by GitHub
/*child.js*/
// Import necessary modules and decorators from LWC
import { LightningElement, api } from 'lwc';
// Import Apex method for fetching account data
import getAccounts from '@salesforce/apex/AccountController.fetchAccounts';
// Define the base URL for the external API
const apiurl = 'https://api.wheretheiss.at/v1/coordinates/&#39;;
// Export the Child component class
export default class Child extends LightningElement {
// Property to store value passed from parent using an HTML attribute
valuePassedFromParentUsingAttribute;
// Reactive property to store accounts fetched from Apex
accounts = [];
// Public property to receive value from the parent using a property binding
@api valuePassedFromParentUsingProperty;
// Reactive properties to store latitude and longitude
latitude;
longitude;
// Object to store location information fetched from the external API
locationInfo;
/**
* Constructor initializes the component's properties.
*/
constructor() {
super();
//Set Default latitude
this.latitude = "40.6892";
//Set Default longitude
this.longitude = "-74.0445";
// Initialize location info as an empty array
this.locationInfo = [];
}
/**
* Lifecycle hook invoked when the component is inserted into the DOM.
* Sets up event listeners, fetches accounts, and makes external API calls.
*/
connectedCallback() {
// Fetch data from Apex
getAccounts()
.then((result) => {
// Store the fetched accounts
this.accounts = result;
console.log("ConnectedCallback: Fetched accounts=", JSON.stringify(this.accounts));
})
.catch((error) => {
// Store any error
this.error = error;
console.error("ConnectedCallback: Error fetching accounts=", error);
});
// Add a click event listener to the host element
this.addEventListener("click", this.handleHostClick);
// Add a CSS class to the host element for styling purposes
this.classList.add("child-host");
// Access the host element
const host = this.template.host;
// Set a custom data attribute on the host element
host.dataset.custom_attr = "Data from child";
// Read an attribute provided by the parent component
this.valuePassedFromParentUsingAttribute = host.getAttribute("data-id");
console.log("ConnectedCallback: Parent provided data using attribute=", this.valuePassedFromParentUsingAttribute);
// Attempt to access child elements (not yet available at this point in the lifecycle)
console.log("ConnectedCallback: button =", this.template.querySelector("lightning-button")); // Logs null
// Fetch location data from an external API
this.fetchLocationData();
}
/**
* Handle click events on the host element.
* Dispatches a custom event to notify the parent component.
*/
handleHostClick = () => {
console.log('Host element clicked!');
// Dispatch a custom event named 'hostclick'
this.dispatchEvent(new CustomEvent('hostclick'));
};
/**
* Lifecycle hook invoked when the component is removed from the DOM.
* Cleans up any resources or event listeners added in connectedCallback.
*/
disconnectedCallback() {
// Remove the click event listener from the host element
this.template.host.removeEventListener('click', this.handleHostClick);
}
/**
* Fetch location data from an external API using latitude and longitude.
* Handles errors and populates the locationInfo object with the API response.
*/
fetchLocationData() {
if (this.latitude && this.longitude) {
/* Ensure the external API's URL is added as a Trusted URL in Salesforce:
Setup >> Trusted URLs >> New Trusted URL.
Select the "connect-src" checkbox to allow external fetch calls in LWC. */
fetch(apiurl + this.latitude + ',' + this.longitude)
.then(response => {
console.log("ConnectedCallback: response from server =", JSON.stringify(response));
if (response.ok) {
return response.json(); // Parse the response as JSON
}
throw Error(response); // Handle HTTP errors
})
.then(data => {
console.log("ConnectedCallback: response from server =", JSON.stringify(data));
// Populate locationInfo with data from the API response
this.locationInfo = {
country_code: data.country_code,
timezone_id: data.timezone_id,
latitude: data.latitude,
longitude: data.longitude,
map_url: data.map_url
};
})
.catch(error => console.log(error)); // Log any errors
}
}
/**
* Getter to check if location information is populated.
* @return {boolean} – Returns true if locationInfo has timezone_id, false otherwise.
*/
get locationPopulated() {
return this.locationInfo && this.locationInfo.timezone_id;
}
}
view raw child.js hosted with ❤ by GitHub
<!–parent.html–>
<template>
<c-child data-id="CHILD-123" class="parent-supplied-class" value-passed-from-parent-using-property="testValue"
onhostclick={handleHostClick}></c-child>
<lightning-card title="Parent Component Connected Call Back Example – LWC">
<div style="padding: 1rem;">
<p>Parent Value: {parentValue}</p>
</div>
</lightning-card>
</template>
view raw parent.html hosted with ❤ by GitHub
/*parent.js*/
// Import the base LightningElement class from the LWC framework
import { LightningElement } from "lwc";
// Export the ParentComponent class
export default class ParentComponent extends LightningElement {
// Reactive property to hold the value updated by the child component
parentValue = "";
/**
* Event handler for the custom 'hostclick' event dispatched by the child component.
* Updates the parentValue property based on the data attribute of the clicked child component.
* @param {Event} event – The event object containing details of the click.
*/
handleHostClick(event) {
// Use currentTarget to reference the child component's host element
const hostElement = event.currentTarget;
// Update the parentValue with information from the child component's data attribute
this.parentValue = `Child Component clicked! : ${hostElement.dataset.custom_attr}`;
}
}
view raw parent.js hosted with ❤ by GitHub
ProsCons
Ideal for initializing tasks like data fetching and setting up listeners.Overuse can lead to performance bottlenecks if the hook is fired multiple times (e.g., during re-insertion).
Executes after all public properties are assigned.Not suited for reactive property updates—use a setter instead.
Handles dynamic updates like subscribing to message channels.Adding heavy computations here can delay rendering
Allows interaction with parent elements and external systems.Can’t access child elements, as they are not yet in the DOM.

3. renderedCallback()

  • Key Points:
    1. Flows from child to parent component i.e. if you have a parent and child component, the renderedCallback() defined in the child component will fire first.
    2. renderedCallback() runs multiple times, as a component is usually rendered many times during the lifespan of an application. To avoid repeated execution, use a boolean flag (e.g., isRendered) to track if renderedCallback() has already executed.
    3. Can be used for below, but make sure to use guard logic using boolean flag to guarantee a one-time run of renderedCallback()-
    4. Perform logic after a component has finished rendering. This is useful for tasks related to UI interaction. Examples include computing node sizing or adding event listeners.
    5. Avoid updating component state (e.g., @track or public properties) within renderedCallback(), as it can lead to infinite rendering loops.Instead, use getters and setters for reactive changes.
    6. Child elements can be accessed using this.template.querySelector() and this.template.querySelectorAll().
    7. Event listeners added in renderedCallback won’t duplicate, as browsers ignore identical listeners. However, it’s best practice to remove them in disconnectedCallback to avoid potential memory leaks.
    8. Reference Link for for some more renderedCallback() use case examples – Link
Rendered Call back example Code –
  • Rendered Call Back Ex LWC component shows example of below functionalities –
    1. Avoid repeated execution of renderedCallback():
      • Usage of boolean property “hasRendered” to run renderedCallback() only one time.
    2. Loading Custom Style From Static Resource Load Style ‘lightning/platformResourceLoader’:
      • Loading custom style from static resources and adding custom style on “sample button” after component has rendered.
    3. Adding Dynamic Event Listner After The Component is Rendered:
      • Adding “click” event listener to “sample button” dyamically in js.
/* Custom css needed to be added to static resources named customButtonStyle*/
.custom-lightning-button {
background-color: #ff5733; /* Custom background color */
color: white; /* Custom text color */
border-radius: 8px; /* Rounded corners */
font-size: 16px; /* Adjust font size */
padding: 10px 20px; /* Custom padding */
}
<!–renderedCallBackEx.html–>
<template>
<lightning-card title="Rendered Call Back Example – LWC">
<div style="padding: 1rem;">
<lightning-button class="custom-lightning-button" label="Sample Button"></lightning-button>
</div>
</lightning-card>
</template>
/*renderedCallBackEx.js*/
// Import necessary modules from the LWC framework
import { LightningElement } from 'lwc';
// Import the `loadStyle` function to dynamically load styles
import { loadStyle } from 'lightning/platformResourceLoader';
// Import custom button styles from a static resource
import customButtonStyle from '@salesforce/resourceUrl/customButtonStyle';
// Export the RenderedCallBackEx class
export default class RenderedCallBackEx extends LightningElement {
// Internal property to track if the rendered callback logic has already been executed
hasRendered;
/**
* Constructor is called when the component instance is created.
* Used for initial setup, such as initializing private properties.
*/
constructor() {
super();
// Initialize the `hasRendered` property to false
this.hasRendered = false;
}
/**
* Lifecycle hook invoked after the component’s template has been rendered.
* Used for post-rendering logic, such as applying custom styles or adding event listeners.
*/
renderedCallback() {
// Ensure the logic executes only once during the component's lifecycle
if (!this.hasRendered) {
// Load the custom CSS from the static resource
loadStyle(this, customButtonStyle)
.then(() => {
console.log('renderedCallback – Custom styles loaded successfully.');
})
.catch((error) => {
console.error('renderedCallback – Error loading custom styles:', error);
});
// Dynamically bind the click event handler to the button
const button = this.template.querySelector('lightning-button');
console.log('renderedCallback – button', button);
if (button) {
button.addEventListener('click', this.handleClick);
}
// Mark that the rendered callback logic has been executed
this.hasRendered = true;
}
}
/**
* Event handler for button clicks.
* Logs a message to the console when the button is clicked.
*/
handleClick = () => {
console.log('handleClick – Sample Button clicked dynamically!');
};
}
ProsCons
Ensures the DOM is fully rendered, making it ideal for DOM manipulations.Updating reactive properties here can cause infinite loops.
Perfect for initializing third-party libraries that rely on DOM elements.Can fire multiple times, requiring careful handling to prevent redundant operations.
Allows one-time tasks using flags (e.g., hasRendered boolean).Poor design can lead to unnecessary re-render cycles, affecting performance.
Supports dynamic behaviors like measuring or modifying DOM content.Not suitable for heavy logic that impacts rendering performance.

4. disconnectedCallback()

  • Key Points:
    1. Flows from child to parent component i.e. if parent and child component are removed, the disconnectedCallback() defined in the child component will fire first.
    2. Can be used to clean up work done in the connectedCallback() like –
    3. Important to release resources: Ensures efficient memory usage and prevents unintended behavior in the application
    4. Runs once for each component removal: Executes each time the component is removed from the DOM. Add logic to avoid redundant cleanup if necessary.
Disconnected Call Back Example Code –

The example LWC showcases the below functionality –

  • Child Component: Shows the below examples in disconnectedCallback():
    1. Setting up an interval and event listener
      • The child component adds a click event listener to its host element. When clicked, it triggers the handleHostClick method, which dispatches a custom event named hostclick.
      • In the connectedCallback lifecycle hook, an interval is created using setInterval, which logs a message to the console every second. This simulates a recurring task.
    2. Handling resource cleanup in disconnectedCallback()
      • Interval Cleanup: When the child component is removed from the DOM, the disconnectedCallback lifecycle hook clears the interval. It uses clearInterval to prevent memory leaks.
      • Event Listener Removal: The click event listener is removed from the host element. This ensures no residual listeners remain after the component is disconnected.
  • Parent component
    1. The parent component toggles the visibility of the child component using the show property. The element is conditionally rendered based on this property using the <template if:true> directive.
    2. A button in the parent component toggles the show property. This allows users to dynamically add or remove the child component from the DOM.
<!–disConnectedCallbackChildLwc.html–>
<template>
<p>Child Component</p>
</template>
/*disConnectedCallbackChildLwc.js*/
import { LightningElement } from 'lwc';
export default class DisConnectedCallbackChildLwc extends LightningElement {
intervalId;
/**
* Constructor initializes the component's properties.
*/
connectedCallback() {
// Add a click event listener to the host element
this.addEventListener("click", this.handleHostClick);
/* eslint-disable @lwc/lwc/no-async-operation */
// Setting up an interval
this.intervalId = setInterval(() => {
console.log("ConnectedCallback: Interval running");
}, 1000);
}
/**
* Handle click events on the host element.
* Dispatches a custom event to notify the parent component.
*/
handleHostClick = () => {
console.log('Host element clicked!');
// Dispatch a custom event named 'hostclick'
this.dispatchEvent(new CustomEvent('hostclick'));
};
/**
* Lifecycle hook invoked when the component is removed from the DOM.
* Cleans up any resources or event listeners added in connectedCallback.
*/
disconnectedCallback() {
// Remove the click event listener from the host element
this.template.host.removeEventListener('click', this.handleHostClick);
// Clear the interval
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
console.log('disconnectedCallback called');
}
}
<!–disConnectedCallbackParentLwc.html–>
<template>
<lightning-card title="Disconneced Call Back Example – LWC">
<lightning-button variant="brand" slot="actions" label="Show/Hide" onclick={handleShowHide}
class="slds-m-left_x-small"></lightning-button>
<div style="padding: 1rem;">
<p>Parent LWC component</p>
<template if:true={show}>
<c-dis-connected-callback-child-lwc></c-dis-connected-callback-child-lwc>
</template>
</div>
</lightning-card>
</template>
/*disConnectedCallbackParentLwc.js*/
import { LightningElement } from 'lwc';
export default class DisConnectedCallbackParentLwc extends LightningElement {
show = true;
handleShowHide() {
this.show = !this.show;
}
}
ProsCons
Essential for cleanup tasks like removing event listeners or caches.Can be overlooked, leading to memory leaks or orphaned resources.
Helps maintain performance in long-lived applications.May not always fire in cases of abrupt application terminations (e.g., browser crash).
Complements connectedCallback() for resource management symmetry.Mismanagement of cleanup logic can lead to unintended behavior when components are reconnected.

5. errorCallback(error, stack)

  • Key Points:
    1. Similar to JavaScript catch{} block, has two parameters.
    2. Triggered if a descendant component (child or nested child) in the same DOM tree throws an error during the rendering or lifecycle phases
    3. To capture the stack information and render a different template when error is occurred.
    4. Can be used for:
    5. Handles errors only for child components within the current component’s DOM subtree.
    6. Does not catch all errors like Errors thrown asynchronously (e.g., in a setTimeout) or errors outside the component tree, like global event handlers
Error Call back example Code –

The example LWC showcases the below functionality –

  • Before errorCallback() in Parent Component – Let’s suppose we have an error in the child component. This occurs due to the child component setting a custom property to the host element. If the parent component doesn’t have an errorCallback(), the child component will not display on the UI. An error message will be displayed as. –

errorCallBackChildLwc.html

<template>
    <div style="padding: 1rem;">
        <p>Child Component</p>
    </div>
</template>

errorCallBackChildLwc.js

import { LightningElement } from 'lwc';

export default class ErrorCallBackChildLwc extends LightningElement {

    connectedCallback() {
        // Access the host element
        const host = this.template.host;

        // Set a custom data attribute on the host element
        this.template.dataset.custom = "Data from child using custom attributes";
    }
}

errorCallBackParentLwc.html

<template>
    <lightning-card title="Error Call Back Example - LWC">
        <div style="padding: 1rem;">
            <p>Parent Component</p>
            <c-error-call-back-child-lwc onhostclick={handleHostClick}></c-error-call-back-child-lwc>
        </div>
    </lightning-card>
</template>

errorCallBackParentLwc.js

import { LightningElement } from 'lwc';

export default class ErrorCallBackParentLwc extends LightningElement {
}
  • After errorCallback() in Parent Component – The error in the child component is handled in the parent component’s errorCallback(). The child component will be rendered on UI.
import { LightningElement } from 'lwc';

export default class ErrorCallBackParentLwc extends LightningElement {

    errorCallback(error, stack){
        console.log('Parent error callback called, error = ' + error + ', stack = ' + stack);
    }
}
ProsCons
Acts as an error boundary, capturing unhandled errors in descendant components.Only catches errors from template handlers or lifecycle hooks—not programmatically attached events.
Provides error and stack trace details for debugging.Must be implemented thoughtfully to avoid exposing sensitive error information in logs or UI.
Allows graceful degradation of UI by rendering alternative views.Relies on manual implementation to wrap components—may be inconsistent if not uniformly applied.
Prevents crashes by catching exceptions and preserving application stability.Can add complexity when handling deeply nested component hierarchies.

LWC Lifecycle Flow –

Lightning web components have a lifecycle managed by the framework. The framework creates components, inserts them into the DOM, renders them, and removes them from the DOM. It also monitors components for property changes.

The below diagram shows the flow of the component lifecycle from creation through render.

component lifecycle from creation through render

The below diagram shows what happens when a component instance is removed from the DOM.

component instance is removed from the DOM
component instance is removed from the DOM

Leave a comment