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

97
Views
La ejecución automática de MobX no obtiene la dependencia que debería

Este código lo saqué de 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");

Pero en consola me sale esto:

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

No recoge message.title como dependencia, ¡ayúdame a entender por qué!

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

0

De manera predeterminada make(Auto)Observable solo admite propiedades que ya están definidas, por lo que debe definir cada propiedad dentro del constructor o hacer que sea anulable así: author = null , o agregue el valor predeterminado 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; } }

Alternativamente, es posible que desee intentar reconfigurar cómo funciona la inicialización de propiedades de clase , utilizando el indicador del compilador useDefineForClassFields TS:

 "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!