constructor()
β Initializationπ When it runs: When component is created, before it hits the DOM.
π‘ Use Case: Initialize default values. Avoid DOM access here.
constructor() {
super();
this.filterCriteria = 'Active Students';
}
β Great for setting internal state, NOT for fetching data.
connectedCallback()
β DOM Entry Pointπ When it runs: When component is inserted into the DOM.
π‘ Use Case: Fetch data, call Apex, subscribe to events.
connectedCallback() {
getStudents()
.then(result => {
this.students = result;
})
.catch(error => {
this.error = error;
});
}
β Perfect for Apex calls, pub-sub setup, listeners.
renderedCallback()
β After Every Renderπ When it runs: Every time DOM is rendered.
π‘ Use Case: DOM manipulations, scroll handling, third-party library init.
renderedCallback() {
if (!this.isRendered) {
this.template.querySelector('lightning-input').focus();
this.isRendered = true;
}
}
β οΈ Use carefully β avoid infinite loops.
disconnectedCallback()
β Cleanup Timeπ When it runs: When component is removed from DOM.
π‘ Use Case: Unsubscribe, clear intervals, disconnect observers.
disconnectedCallback() {
unsubscribe(this.subscription);
this.subscription = null;
}
β Prevents memory leaks.
errorCallback(error, stack)
β Error Handlingπ When it runs: If error occurs in lifecycle methods or child components.
π‘ Use Case: Debugging + fallback UI.
errorCallback(error, stack) {
console.error('Error in LWC:', error, stack);
this.errorMessage = 'Something went wrong! Please try again later.';
}
β Helps you fail gracefully instead of breaking UI.
Lifecycle Hook | Timing | Common Use Case |
---|---|---|
constructor() | On creation | Initialize state, default values |
connectedCallback() | On DOM insertion | Fetch data, setup subscriptions |
renderedCallback() | After every render | DOM manipulation, libraries |
disconnectedCallback() | On DOM removal | Cleanup, unsubscribe |
errorCallback() | On error in component | Graceful error handling |
Imagine a Student Attendance Dashboard in Salesforce:
This ensures a smooth user experience, without memory leaks or runtime errors.
β
Keep constructor() light β No DOM, no heavy logic.
β
Use connectedCallback() β For API calls & subscriptions.
β
renderedCallback() with caution β Guard with flags to avoid loops.
β
Always cleanup in disconnectedCallback() β Prevent leaks.
β
errorCallback() = friend β For user-friendly debugging.
Lifecycle hooks arenβt just technical jargon β theyβre the rhythm of your LWCβs life. Mastering them will help you:
β Build performant components
β Reduce bugs & crashes
β Deliver enterprise-grade Salesforce apps
π Next time you write an LWC, think: Which lifecycle hook is best for this job?
constructor()
in LWC? Can you call Apex methods inside it?constructor()
and connectedCallback()
. Which one is better for fetching data?connectedCallback()
is the right place for Apex calls and subscriptions.constructor()
?renderedCallback()
differ from connectedCallback()
?renderedCallback()
executes after rendering and can run multiple times, while connectedCallback()
runs only once when added to the DOM.renderedCallback()
?this.isRendered
) to ensure DOM logic executes only once.disconnectedCallback()
? Give a real-life scenario.errorCallback()
. How does it help in debugging and user experience?constructor()
for defaults, connectedCallback()
for fetching records, renderedCallback()
for scroll handling, disconnectedCallback()
for cleanup, and errorCallback()
for handling chart errors.π Trusted by 1000+ learners to crack interviews at TCS, Infosys, Wipro, EY, and more.
Here are some resources to go deeper:
πΒ 100 Scenarios (1β4 YOE)
πΒ 100 Scenarios (4β8 YOE)
πΒ LWC Q&A (Real Answers Explained)
πΒ 600+ Qs from Recruiter Calls
πΒ TCS, Infosys, EY Interview Qs
πΒ Salesforce Project β Hindi + English