在我的React应用程序中,mobx @action没有触发-mobx,mobx-react-lite

问题描述

我第一次使用mobxmobx-react-lite,可以看到@action没有触发任何事件。

请找到Code SandBox url here

我的意图是当我单击按钮时,名称CHAN应该更改为XIAN。但这没有发生。您能告诉我我做错了什么吗?预先感谢。

解决方法

您在myStore构造函数中缺少struct struct_B{ struct_A *mystructA; } struct_B mystructB; function( (&mystructB)->mystructA ); //this line cause me a segfault

Codesandbox

Documentation

makeObservable
,

除了@Christiaan答案外,您还可以使用makeAutoObservable并完全删除装饰器!

import { observable,action,makeAutoObservable } from "mobx";

export class myStore {
  name: string = "CHAN";

  constructor() {
    makeAutoObservable(this)
  }

  setName(newName: string) {
    this.name = newName;
  }
}

更多信息:https://mobx.js.org/react-integration.html