# What are the primary LWC lifecycle hooks, and in what order do they execute?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Lifecycle Hooks, Component Lifecycle

Read it on the site: https://www.forcechallenges.com/interview-questions/what-are-the-primary-lwc-lifecycle-hooks-and-in-what-order-do-they-execute

## Answer

**Basics:** Lifecycle hooks are methods the platform calls at defined moments so you can initialize, touch the DOM, or clean up. They are standard methods on a class extending `LightningElement`.

**How it works:**
1. `constructor()`: instance created. Must call `super()` first. The component is not yet attached, so you cannot read attributes, children or public properties passed from the parent.
2. `connectedCallback()`: inserted into the DOM. Public properties are set; do initialization here, subscribe to messages, add listeners, kick off imperative Apex. The template does not exist yet, so `this.template.querySelector` returns null.
3. `render()` (optional): returns which template to use; template is evaluated.
4. Children of the template are created and go through their own constructor, connected and render steps.
5. `renderedCallback()`: runs after render, child first, then parent. Use for DOM-dependent work, with a guard.
6. `disconnectedCallback()`: removed from DOM; unsubscribe and clear timers.
7. `errorCallback(error, stack)`: on an ancestor, catches errors from descendants.

**Advanced / deep dive:** Order for nested components: parent constructor, parent connected, parent render, child constructor, child connected, child render, child rendered, parent rendered. Reconnection: if a component is removed and reinserted (for example by `lwc:if` toggling), `connectedCallback` and `disconnectedCallback` fire again, but class fields initialized earlier are retained only if the same instance is reused; with conditional removal a fresh instance is built. `renderedCallback` fires after every re-render, not just the first. Wire adapters provision after connection and may emit before or after first render, so never assume data is present in `connectedCallback`. Avoid heavy synchronous work in the constructor and connected phases, since it delays first paint.

**Code / config example:**
```javascript
import { LightningElement } from 'lwc';
export default class Demo extends LightningElement {
    constructor() { super(); console.log('1 constructor'); }
    connectedCallback() { console.log('2 connected'); }
    renderedCallback() { console.log('3 rendered'); }
    disconnectedCallback() { console.log('4 disconnected'); }
}
```

**Hidden gotchas & must-remember points:**
- Forgetting `super()` in the constructor is an error; do not read `this.template` or attributes there.
- `connectedCallback` can run more than once per instance in some reinsertion cases, so make setup idempotent.
- Always undo in `disconnectedCallback` what you did in `connectedCallback` (subscriptions, intervals, global listeners) to avoid leaks.
- `renderedCallback` runs after every render; changing state in it can loop forever.
- `errorCallback` does not catch errors in the component itself, nor async errors.
- Child `renderedCallback` runs before the parent's.

**Best practice:** Initialize in `connectedCallback`, touch the DOM in a guarded `renderedCallback`, and always clean up in `disconnectedCallback`.

---
Source: ForceChallenges · last updated 2026-11-02
