使用带有类实例作为属性的 mobx 存储时未定义的函数

问题描述

我正在使用 mobx 和 decimal.js。
这是我的商店:

import Decimal from "decimal.js";
import { makeObservable,observable,action } from "mobx";

class MyStore {
  public value: Decimal | null = null;

  constructor() {
    makeObservable(this,{
      value: observable,setValue: action,});
  }

  public setValue() {
    this.value = new Decimal(100);
  }
}

export { MyStore };

这是我的组件:

import { useStoreValue } from "../../state/StoreContext";
import { observer } from "mobx-react-lite";

const MyPage = observer(() => {

  const value = useStoreValue((rootStore) => rootStore.myStore.value);

  return <span>{value.mul(5)}</span>;
});

export { MyPage };

结果我得到以下异常:

uSEObserver.ts:119 Uncaught TypeError: _value.mul is not a function

知道我错过了什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)