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

前端 未结 3 1901
南方客
南方客 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:13

    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)}
    

提交回复
热议问题