# How does the LWC reactivity engine manage property observation and virtual DOM updates?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Reactivity Engine, Virtual DOM

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-the-lwc-reactivity-engine-manage-property-observation-and-virtual-dom-updates

## Answer

**Basics:** Since Spring '20, all class fields are reactive for primitive values: if a field is used in the template (or in a getter the template uses) and its value changes, the component re-renders. For objects and arrays, reactivity applies to reassignment (`this.list = [...this.list, item]`), not to in-place mutation, unless the field is decorated with `@track`.

**How it works:** During `render()` the engine records which reactive fields and `@api` properties are accessed. A set operation on one of them (or a mutation seen through the `@track` proxy) marks the component dirty. Dirty components are queued and re-rendered once in a microtask, so several changes in one tick cause a single render. The template produces a virtual DOM tree, which is diffed against the previous one (keyed for `for:each`), and only changes are written to the real DOM. Then `renderedCallback` fires.

**Advanced / deep dive:** `@track` uses an observable membrane (Proxy-based) that wraps plain objects and arrays deeply, so mutations such as `this.obj.a.b = 1` or `this.arr.push(x)` are detected. Class instances, `Date`, `Map` and `Set` are not deeply tracked. Values passed from a parent via `@api` are read-only proxies, so a child cannot mutate them; it must copy or dispatch an event. Fields not read during render do not cause re-renders when changed, which is an optimization (use plain non-template fields for internal state). Getters have no memoization: they run every render. Wire results are frozen. Infinite loops come from changing reactive state in `renderedCallback` unconditionally. For performance, avoid creating new arrays and objects each render in getters, and use stable keys so the diff can reuse nodes.

**Code / config example:**
```javascript
import { LightningElement, track } from 'lwc';

export default class Cart extends LightningElement {
    count = 0;                      // primitive: reactive when reassigned
    items = [];                     // reactive by reassignment only
    @track filter = { text: '' };   // deep reactive via proxy

    add(item) {
        this.items = [...this.items, item];  // triggers re-render
        // this.items.push(item);             // would NOT trigger without @track
        this.count++;
    }
    setText(t) { this.filter.text = t; }     // detected thanks to @track
}
```

**Hidden gotchas & must-remember points:**
- Mutating an untracked object or array does not re-render; reassign.
- `@track` is only needed for deep in-place mutation of plain objects and arrays.
- `@api` values from a parent are read-only in the child.
- Fields not used in the template do not trigger renders.
- Renders are batched; DOM updates are not synchronous with assignment.
- Unconditional state changes in `renderedCallback` cause infinite loops.

**Best practice:** Prefer immutable updates (spread, map, filter) and reassign, using `@track` only when you deliberately mutate nested structures.

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