Lifecycle Hooks in LWC & Basic Demo | Peoplewoo Skills

21.11.25 10:10 AM - By Peoplewoo

Lightning Web Components (LWC) follow a predictable lifecycle—starting from creation, rendering, re-rendering, and ending with component destruction. Understanding these lifecycle hooks is essential for controlling behavior, calling APIs at the right time, improving performance, and building stable applications.


What Are Lifecycle Hooks in LWC?

Lifecycle Hooks are predefined callback methods in JavaScript that execute automatically at specific stages of a component’s life. These hooks allow developers to:

  • Initialize data
  • Call Apex methods
  • Handle DOM-related logic
  • Clean up event listeners


Watch Our Video Tutorial

List of Lifecycle Hooks in LWC


Lifecycle HookWhen It Runs
constructor()When the component instance is created
connectedCallback()When the component is inserted into the DOM
renderedCallback()After the component is rendered
disconnectedCallback()When the component is removed from the DOM
errorCallback()Handles errors thrown in child components


1. constructor()


The first hook executed when an LWC component instance is created. Best used for initializing basic variables.

 constructor() {     super();     console.log('Constructor called!'); } 

Restrictions: You cannot access DOM elements here.


2. connectedCallback()


Called when the component is inserted into the DOM. Use it to:

  • Call Apex methods
  • Initialize data
  • Register event listeners
 connectedCallback() {     console.log('Component loaded in DOM'); } 


3. renderedCallback()


Executes every time the component renders or re-renders. Use this for DOM manipulation.

 renderedCallback() {     console.log('Component rendered!'); } 

Note: It may run multiple times, so avoid heavy logic here.


4. disconnectedCallback()


Triggered when a component is removed from the DOM. Used for cleanup work.

 disconnectedCallback() {     console.log('Component removed!'); } 

Examples of cleanup:

  • Removing event listeners
  • Clearing timers
  • Unsubscribing from events


5. errorCallback(error, stack)

Captures errors from child components.

 errorCallback(error, stack) {     console.error('Error:', error);   
  console.log('Stack:', stack); } 


Basic Demo of Lifecycle Hooks


Below is a simple LWC example demonstrating all lifecycle hooks:

 import { LightningElement } from 'lwc'; export default class LifecycleDemo
 extends LightningElement {     constructor() {         super();       
  console.log('Constructor executed');     }     connectedCallback() 
{         console.log('connectedCallback executed');     }     
renderedCallback() {         console.log('renderedCallback executed'); 
    }     disconnectedCallback() {        
 console.log('disconnectedCallback executed');     } 
    errorCallback(error, stack)
 {         console.log('Error caught:', error);     } } 


Real-Life Use Case


Example: Fetching Account data when the component loads


Developers often use connectedCallback() to fetch Apex data as soon as the component appears on the screen.

 connectedCallback() {     getAccountList()         .then(result => 
{             this.accounts = result;         })         .catch(error => 
{             console.error(error);         }); } 

This ensures the data loads before the user interacts with the component.


Best Practices


  • Use constructor only for variable initialization
  • Use connectedCallback for data fetching and event registration
  • Avoid heavy logic inside renderedCallback
  • Always clean up listeners in disconnectedCallback
  • Use errorCallback for debugging child components

Frequently Asked Questions (FAQ)

1. Which lifecycle hook should I use for Apex calls?

connectedCallback() is best for Apex calls because it runs only once when the component is attached to the DOM.

2. Why does renderedCallback() run multiple times?

It executes every time the component re-renders, such as after value changes.

3. Can I access DOM elements in constructor()?

No. DOM is not yet created during constructor execution.

4. What is the use of disconnectedCallback()?

It helps in cleanup — removing listeners, clearing intervals, and releasing memory.

5. How do I handle errors coming from child components?

Use errorCallback(error, stack) to capture and log errors.

Conclusion


Lifecycle hooks are essential for building efficient, reactive, and stable Lightning Web Components. Understanding when each hook executes allows you to structure logic correctly—whether fetching data, updating UI, or handling cleanup. Master these hooks to level up your LWC development skills.


More SFDC Resources


Start your SFMC journey today — join our Live Training 

Need help? Chat with us on WhatsApp anytime.

Learn. Practice. Get Certified. Succeed with Peoplewoo Skills.

Peoplewoo

Peoplewoo

Peoplewoo Consulting Private Limited
https://www.peoplewoo.com/