微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mobx@6.x 类存储无反应

如何解决mobx@6.x 类存储无反应

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

示例:

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

解决方法

您需要立即初始化您的字段,或者如果您想在构造函数中初始化它们,那么您需要在所有初始化之后调用 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。