Skip to main content

makeObservable and makeObservable

What makeObservable() does

makeObservable(this, annotations) - makes a class instance's properties and methods observable, according to the given annotations.

You manually specify which fields and methods become:

  • observable - tracked for changes,
  • computed - calculated based on other observables,
  • action - change the state.

Example with makeObservable

javascript
import { makeObservable, observable, action, computed } from "mobx" class Counter { count = 0 constructor() { // manually declare what to observe makeObservable(this, { count: observable, increment: action, double: computed, }) } get double() { return this.count * 2 } increment() { this.count++ } } const counter = new Counter()

Now:

  • count is a tracked value,
  • increment() is an "action",
  • double is a computed property.

Components wrapped in observer will automatically re-render if count changes.


Key points:

  • makeObservable() is an explicit description of reactive properties.
  • You need to specify annotations manually.
  • It fits when you need fine-grained control (for example, not all fields are observable).
  • It also works with inheritance (annotations from different classes can be combined).

What makeAutoObservable() does

makeAutoObservable(this) - automatically makes all of a class's properties and methods reactive, without explicitly listing annotations.

That is, MobX determines on its own what is:

  • observable (fields),
  • computed (getters),
  • action (methods).

Example with makeAutoObservable

javascript
import { makeAutoObservable } from "mobx" class Counter { count = 0 constructor() { makeAutoObservable(this) // everything is done automatically } get double() { return this.count * 2 } increment() { this.count++ } } const counter = new Counter()

Here MobX itself:

  • made countobservable,
  • doublecomputed,
  • increment()action.

Why "auto"

MobX analyzes the class structure:

  • fields → observable,
  • getters → computed,
  • methods → action.

You do not need to specify anything manually. This is the most convenient way to create a store for most scenarios.


The difference between makeObservable and makeAutoObservable

PropertymakeObservablemakeAutoObservable
AnnotationsSpecified manuallyDetermined automatically
ControlFull (flexibility)Simplified (convenience)
Fits forComplex, custom structuresSimple stores and UI state
ComputedMust be declared manuallyDetermined from getters
ActionsSpecified manuallyMarked automatically
Used more oftenIn large projects with type controlIn most cases (UI / SPA)

Example of the difference

javascript
class UserStore { name = "Alex" age = 25 constructor() { // makeObservable requires an explicit description makeObservable(this, { name: observable, age: observable, isAdult: computed, birthday: action, }) } get isAdult() { return this.age >= 18 } birthday() { this.age++ } }

The same thing with makeAutoObservable:

javascript
class UserStore { name = "Alex" age = 25 constructor() { makeAutoObservable(this) } get isAdult() { return this.age >= 18 } birthday() { this.age++ } }

Both variants work the same way, but the second one is shorter, cleaner, and simpler.


When to use which

ScenarioWhat to choose
90% of cases - standard storesmakeAutoObservable
Need to exclude a field from observationmakeObservable (explicit control)
There are fields that don't need reactivity (for example, timeoutId, config)makeObservable
Complex inheritance between store classesmakeObservable
Quickly create a store for the UImakeAutoObservable

Combining them is possible

You can use makeAutoObservable, but still specify what should not be tracked:

javascript
class Store { id = Math.random() value = 0 constructor() { makeAutoObservable(this, { id: false, // do not track this property }) } increment() { this.value++ } }

SUMMARY

makeObservable(this, annotations) - manual setup of reactive properties. makeAutoObservable(this) - automatic reactivity "out of the box".

FunctionWhat it does
makeObservablemanually declares observable, computed, and action
makeAutoObservableautomatically makes everything observable

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.