【问题标题】:mobx@6.x class store no reactivemobx@6.x 类存储无反应
【发布时间】:2021-03-16 23:45:21
【问题描述】:

当我使用 mobx@6.x 与类类型存储不反应

示例:

https://codesandbox.io/s/mobx-react-class-store-demo-xzcuv?file=/src/App.tsx

【问题讨论】:

  • 请提供更多解释

标签: reactjs mobx mobx-react


【解决方案1】:

你需要立即初始化你的字段,或者如果你想在构造函数中初始化它们,那么你需要在所有初始化之后调用makeAutoObservable,否则它不会拾取未定义的字段。

class TestStore {
  hello: string;
  // hello = ""; // or just initialize right here with empty string, for example

  constructor() {
    this.hello = "hello world";
    // Call it after all initializations
    makeAutoObservable(this);
  }

  setHello(str: string) {
    console.log("testStore set hello2");
    this.hello = str;
  }
}

更多信息in the docs

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-12-21
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 2016-12-26
  • 2020-05-25
  • 2018-05-03
相关资源
最近更新 更多