Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 39 ยท Tracked State and Reactivity

Tracked Properties

Learn how @tracked makes Ember re-render automatically, what autotracking records during a render, and see the mechanism in a runnable revision-counter model.

Ember's way of keeping the screen up to date with your data is called autotracking, and it is refreshingly simple to use: mark a field with @tracked, read it in a template, and change it whenever you like. Ember re-renders exactly the parts of the page that used the field. You never call setState, never register a watcher and never list dependencies by hand.

This lesson explains what @tracked does, the rules for getting updates reliably, and how the machinery works under the hood, with a runnable model. Understanding the model makes every odd reactivity bug in the next lessons easy to diagnose.

Real Ember vs stand-in. Blocks with a file name are real Ember code, shown read-only. The runnable blocks reproduce the idea (record what a render reads, re-run it when something it read changes) in plain JavaScript.

The simplest tracked component

app/components/counter.gjs

JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { on } from '@ember/modifier';

export default class Counter extends Component {
  @tracked count = 0;

  @action
  increment() {
    this.count++;
  }

  <template>
    <button type="button" {{on "click" this.increment}}>
      Clicked {{this.count}} times
    </button>
  </template>
}

Three things are going on:

  1. @tracked count = 0 turns the field into a property that remembers when it was last changed.
  2. The template reads this.count, so Ember notes "this piece of the output depends on count".
  3. this.count++ assigns a new value, which marks it as changed, so Ember re-renders that text.

@action binds the method to the component so this stays correct when the browser calls it from the event listener.

What gets tracked, and what does not

Only assignments to a @tracked field are noticed. Plain fields (without @tracked) can change all day and the screen will not follow.

JavaScript
export default class Example extends Component {
  @tracked visible = true; // template updates when reassigned
  clicks = 0;              // changes are invisible to the template
}

A tracked field holding an object or array is tracked by reference. Replacing the whole value is noticed. Changing something inside it is not:

JavaScript
@tracked user = { name: 'Ada' };

rename() {
  this.user.name = 'Grace';           // not noticed: same object
  this.user = { ...this.user, name: 'Grace' }; // noticed: new object
}

There are other ways to make nested changes visible (tracked classes, TrackedObject, TrackedArray) and the collections lesson covers them.

How autotracking works

When Ember renders a piece of a template, it keeps a list of every tracked value read during that render. Each tracked value has a revision number that goes up when it is set. After the render, Ember remembers the highest revision it saw. Later it asks: "is any value I read newer than that?". If so, it re-renders that piece, and only that piece.

flowchart LR
  R["Render the template"] -->|reads| T1["count (revision 3)"]
  R -->|reads| T2["name (revision 1)"]
  S["this.count = 5"] -->|"revision becomes 4"| T1
  T1 -.->|"newer than what the render saw"| R2["Re-render only what read count"]

Reading builds the dependency list; writing bumps a revision; a stale dependency triggers a re-render.

The model below is a real working miniature of that idea. A Cell is a tracked value, track() records the cells read by a function, and render() re-runs only when one of them is newer than the last run.

JS Stand-in: a revision-counter autotracker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Only the counter view re-renders after count.set(1). The name view read a different cell, so it is skipped. Ember does exactly this, at the level of individual template expressions, with a much more optimized tag system.

Why there is no dependency list

In some frameworks you declare dependencies by hand (useEffect(..., [count]) or computed('a', 'b')). In Ember you do not, because reading is the declaration. A getter that reads this.firstName and this.lastName automatically depends on both, even if you later add a third read inside a branch. This removes a whole class of bugs ("I forgot to list a dependency"). The cost is the rule that you must read tracked data through the same objects the framework can see, which is why replacing values and using tracked collections matter.

Tracked properties in other places

@tracked is not just for components. It works on any class instance Ember or your code reads from a template:

  • Services: @tracked items = [] in a cart service, shared across the app (Services in Ember).
  • Controllers and routes: for query-parameter backed or page-level state.
  • Plain classes: a class TodoList { @tracked items = [] } that you create yourself works too, because tracking lives in the property, not in the framework object.

You can give a tracked field a default through the initializer (@tracked count = 0) and compute defaults from arguments inside the constructor. Arguments themselves (this.args.title) are already tracked: if the parent passes a new value, the child re-renders, so you never need to copy an argument into a tracked field just to display it.

Do not copy this.args.x into a @tracked field to "keep it up to date". The copy is made once and then goes stale. Read this.args.x directly, or use a getter.

Updating tracked state from async code

Because assignments are noticed no matter where they happen, you can update tracked state after a promise, a timer or an event:

JavaScript
@tracked status = 'idle';

@action
async load() {
  this.status = 'loading';
  try {
    const response = await fetch('/api/items');
    this.items = await response.json();
    this.status = 'done';
  } catch {
    this.status = 'failed';
  }
}

Each assignment bumps a revision; Ember batches the re-renders so the page is updated once per turn, not once per assignment. The next stand-in shows batching with a small scheduler: three assignments, one render.

JS Stand-in: batching renders after several assignments

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Ember notice for a field declared with @tracked?

    Tracking is by assignment. Reassigning the field is noticed; mutating something inside an object it points to is not.
  2. 2. this.user is @tracked and holds { name: 'Ada' }. Which line updates the screen?

    Only assigning a new value to the tracked field is noticed. The other lines change the existing object, which Ember cannot see.
  3. 3. Why does Ember not need dependency lists for getters and templates?

    Autotracking records reads while rendering and compares revisions later, so dependencies are discovered automatically.
  4. 4. A child component shows this.args.title. The parent passes a new title. What should the child do?

    Arguments are tracked automatically. Copying them into your own state usually creates stale data.

Summary

  • @tracked makes a field notify Ember when it is assigned.
  • Templates and getters record every tracked value they read; a newer revision means a re-render of that part only.
  • Replace objects and arrays to make nested changes visible, or use tracked collections.
  • this.args values are already tracked; do not copy them into fields.
  • Several assignments in one turn are batched into one render.

Next: Derived State: Getters and @cached