Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
MobX autorun doesn't get dependency it should

This code I took from https://mobx.js.org/understanding-reactivity.html

import { makeAutoObservable, autorun, getDependencyTree  } from "mobx";

class Message {
  title;
  author;
  likes;
  constructor(title, author, likes) {
    makeAutoObservable(this);
    this.title = title;
    this.author = author;
    this.likes = likes;
  }

  updateTitle(title) {
    this.title = title;
  }
}

let message = new Message("Foo", { name: "Michel" }, ["Joe", "Sara"]);

const disposer = autorun(() => {
  console.log(message.title);
});

console.log(getDependencyTree(disposer));
message.updateTitle("Bar");
message.updateTitle("gasdg");

But in consolse I get this:

Foo
index.js:114 {name: 'Autorun@2'}

It doesn't pick up message.title as dependency, please help me understand why!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

By default make(Auto)Observable only supports properties that are already defined, so you need to define every property inside constructor or make it nullable like that: author = null, or add default value likes = 0:

class Message {
  // Or add default values here
  title = '';
  author = null;
  likes = 0;
  constructor(title, author, likes) {
    // Initialise properties as first thing
    this.title = title;
    this.author = author;
    this.likes = likes;

    makeAutoObservable(this);
  }

  updateTitle(title) {
    this.title = title;
  }
}

Alternatively you might want to try to reconfigure how class properties initialisation works, using useDefineForClassFields TS compiler flag:

"compilerOptions": {
  "useDefineForClassFields": true
},
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!