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

前端 未结 3 888
傲寒
傲寒 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:11

    I don't know mobx but onClick={store.change_me} is a problem because you are using a method on a class as a function (without this). You will have to use use something like:

    onClick={event => store.changeMe(event)}
    

    otherwise the binding to store is lost.

    Also possible but less readable:

    onClick={store.changeMe.bind(store)}
    

提交回复
热议问题