React + Mobx: 'this' is null when trying to update store

前端 未结 3 896
傲寒
傲寒 2021-02-15 10:26

Just getting started with Mobx & React and having trouble getting the store to update. I get error when clicking the button, which should update the \'me\' property:

<
3条回答
  •  心在旅途
    2021-02-15 11:26

    As @Sulthan mentioned, you need to have the method wrapped by another function onClick={()=>store.changeMe()}. Second issue is you are missing action decorator for the method which updating the value. Mobx works in a way where every method which will update properties, it need to be decorated by @action. So following will fix the issue import {action} from 'mobx,

    @action change_me(){
        this.me = 'test 1';
    }
    

提交回复
热议问题