reactjs - 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;
}
}
文档中的更多信息
推荐阅读
- kubernetes - pv 中kubernetes accessModes 的用途 PVC
- r - 字符串未被识别为在 r 中用 stringr 替换
- javascript - 如何编辑发票 Qweb 报告的默认标题。奥多 14
- javascript - javascript出错后如何重置Google OAuth2?
- html - 输入类型数值在角度反应形式中出错
- ruby-on-rails - 用户通过 elasticsearch 创建复杂的查询和搜索
- opengl - 当我将几何着色器添加到着色程序而不是对象不渲染时
- php - PHP文件写入格式
- python - 对没有排列的列表元素
- firebase - 来自Flatlist中Firebase Firestore的数据在React Native中不会呈现