Salesforce Decode
Salesforcedecode
Back to questions
LWCIntermediatelifecyclehooks

Handle LWC lifecycle hooks for cleanup and initialization

Real World Scenario

Component registers interval timer but never clears causing memory leak navigating away.

Expected Answer

• connectedCallback setup subscriptions timers • disconnectedCallback mandatory cleanup unsubscribe clearInterval • renderedCallback avoid infinite loops—guard with flag • errorCallback log component errors centrally optional • Do heavy init in connectedCallback not constructor • Test disconnectedCallback called in Jest unmount

Follow-Up Questions & Answers

Click to expand — each follow-up includes a direct, interview-ready answer

Main difference: use case and scale. connectedCallback setup subscriptions timers. disconnectedCallback mandatory cleanup unsubscribe clearInterval. Pick based on your integration pattern and team capability. disconnectedCallback omitted in most bug reports—mandate in code review checklist. Balance speed of delivery with maintainability.

Architect Perspective

disconnectedCallback omitted in most bug reports—mandate in code review checklist.