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

200
Views
mobX: TypeError: no se pueden leer las propiedades de undefined

Acabo de jugar con un código con mobX, y esto sucedió, no sé por qué, pero estoy seguro de que no es muy diferente de los documentos oficiales.

Aquí está store.tsx

 import { observable, computed, action, makeObservable, override, makeAutoObservable } from "mobx" class CounterStore { initValue = 0 powValue = Math.pow(this.initValue, 2) constructor() { makeAutoObservable(this) } increaseNumber() { this.initValue = this.initValue + 1 } } const Store = new CounterStore() export default Store

Aquí es donde uso esto. llamado aumento.tsx

 import { observer } from "mobx-react" export const IncrementButton = observer(({ store }) => { return ( <div> <button onClick={store.increaseNumber}>Increase</button> <h1>{store.initValue}</h1> </div> ) })

Y index.tsx

 ReactDOM.render( <React.StrictMode> <IncrementButton store={Store} /> {/* <TestUseState /> */} {/* <TestEffect /> <UseMemoTest /> */} </React.StrictMode>, document.getElementById("root"),

Extraño es, muestra initValue , pero cuando hago clic en aumentar, muestra can't not read properties Por favor, ayuda, gracias.

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

0

como notó @mimoid, su método no está vinculado a la clase y pierde contexto ( this ). No es un problema de MobX, es solo una característica regular de javascript llamada enlace tardío .

Aunque realmente no necesita cambiar makeAutoObservable a makeObservable , solo puede usar funciones de flecha, en mi opinión, es una forma más "nativa":

 class CounterStore { initValue = 0 powValue = Math.pow(this.initValue, 2) constructor() { makeAutoObservable(this) } // Just make it an arrow function increaseNumber = () => { this.initValue = this.initValue + 1 } }
about 4 years ago · Juan Pablo Isaza Report

0

Cambie makeAutoObservable a makeObservable y asigne manualmente action.bound a cada acción en lugar de action . Tuve un error similar y lo resolvió.

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!