# What is the role of the @track decorator, and when is it required in modern LWC development?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Reactivity, Decorators

Read it on the site: https://www.forcechallenges.com/interview-questions/what-is-the-role-of-the-track-decorator-and-when-is-it-required-in-modern-lwc-development

## Answer

**Basics:** Reactive means "when this value changes, the template re-renders". Originally all reactive properties needed `@track`. Now a field like `count = 0` is reactive on its own: `this.count++` re-renders. The subtlety is objects and arrays: reassigning `this.obj = {...}` is detected, but `this.obj.name = 'x'` is not unless the field is tracked.

**How it works:**
1. The engine observes only the reference held by a field by default (shallow).
2. With `@track`, the engine wraps the value in a proxy that watches property writes on plain objects and arrays, including nested ones, and schedules a re-render when something the template used changes.
3. Re-render occurs only if the template actually read the changed property during its last render.
4. Rendering is batched in a microtask, so several mutations cause one render.

**Advanced / deep dive:** `@track` observes only plain objects and arrays. Class instances, `Date`, `Map`, `Set` and DOM nodes are not observed deeply, so mutate-and-reassign is required. Many teams avoid `@track` entirely by using immutable updates: `this.items = [...this.items, newItem]` or `this.record = { ...this.record, Name: 'x' }`. Immutable patterns also play well with data from `@wire`, which is frozen (read-only), so mutating it throws in strict mode; you must clone first. Tracked proxies add overhead on large structures, and the proxy is not the original object (identity checks and `JSON.stringify` are fine, but passing the proxy to a third-party library may misbehave, so use `JSON.parse(JSON.stringify(x))` or a structured clone). Properties decorated with `@api` are not deeply reactive either; the parent owns the value and the child must not mutate it.

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

export default class Cart extends LightningElement {
    count = 0;                       // reactive without @track
    @track order = { lines: [] };    // deep mutation observed

    addLine() {
        this.count++;                         // fine, reassignment
        this.order.lines.push({ sku: 'A1' }); // works only because of @track
    }

    // Alternative without @track: immutable update
    addLineImmutable() {
        this.order = { ...this.order, lines: [...this.order.lines, { sku: 'A1' }] };
    }
}
```

**Hidden gotchas & must-remember points:**
- Primitives never need `@track`; adding it to a number or string does nothing useful.
- Adding a brand-new property to an object (`obj.newProp = 1`) is observed only with `@track`; otherwise reassign the object.
- `@track` does not work on `Map`, `Set`, `Date` or class instances.
- Reading a tracked value from `@wire` and mutating it fails because wire data is frozen; copy first.
- A template re-renders only if it reads the changed field; unused state changes cost nothing.
- Old interview answers claiming "all fields need `@track`" refer to pre-Spring '20 behavior.

**Best practice:** Prefer immutable updates and use `@track` only for genuinely nested mutable state.

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