mobx-react observer don't fires when I set observable

后端 未结 1 2045
再見小時候
再見小時候 2020-11-29 13:41

I am trying to create a react application using mobx and typescript. But it doesn\'t work.

I expect the timer to count the seconds. And I see that the event happens a

相关标签:
1条回答
  • 2020-11-29 14:27

    Are you using MobX 6?

    Decorator API changed a little bit from MobX 5, now you need to use makeObservable method inside constructor to achieve same functionality as before:

    import { observable, action, makeObservable } from "mobx";
    
    export class TestStore {
        @observable timer = 0;
    
        constructor() {
          makeObservable(this);
        }
    
        @action timerInc = () => {
            this.timer += 1;
        };
    }
    

    Although there is new thing that will probably allow you to drop decorators altogether, makeAutoObservable:

    import { makeAutoObservable } from "mobx";
    
    export class TestStore {
        timer = 0;
    
        constructor() {
          // Don't need decorators now, just this call
          makeAutoObservable(this);
        }
    
        timerInc = () => {
            this.timer += 1;
        };
    }
    

    More info here: https://mobx.js.org/react-integration.html

    0 讨论(0)
提交回复
热议问题