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

前端 未结 3 1891
南方客
南方客 2021-02-15 10:54

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:23

    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';
    }
    

提交回复
热议问题