reactjsmobxmobx-reactmobx-react-lite

In my React application, mobx @action is not firing - mobx , mobx-react-lite


I am using mobx & mobx-react-lite for first time and I can see the @action is not firing any events.

Please find the Code SandBox url here

My intention is when I click on button, the name CHAN should change to XIAN. But it's not happening. Can you please let me know where I made wrong? Thanks in advance.


Solution

  • You are missing the makeObservable in the myStore constructor.

    Codesandbox

    Documentation

    import { observable, action, makeObservable } from "mobx";
    
    export class myStore {
      @observable name: string = "CHAN";
    
      constructor() {
        makeObservable(this)
      }
    
      @action
      setName(newName: string) {
        this.name = newName;
      }
    }