如何使用 Mobx 更改 'new method' 内的状态

How to change state inside 'new method' with Mobx

我想用同一个状态存储的函数改变状态。
例如,我有如下代码。

import { observable, action } from 'mobx';

export default class AddressTime {
  @observable addressState = {
    departure: {
      road: '',
    }
  };

  departureAddress = () => {
      new window.daum.Postcode({
        oncomplete(data) {

        const roadAddr = data.roadAddress; 

        this.addressState.departure.road = roadAddr;
      })
}

我可以通过 daum post api 制作 roadAddr。 oncomplete 方法中还有一个函数。
所以当我 运行 'departureAddress' 函数说

TypeError:this.addressState is undefined.

这个错误可能是由于一个方法的函数里面this.addressState的位置错误造成的
你能帮我解决这个问题吗?

PS) 在这种情况下我必须使用 mobx 的动作或计算来改变状态吗?

试试这个怎么样?

import { observable, action } from 'mobx';

export default class AddressTime {
    @observable 
    addressState = {
        departure: {
            road: '',
        }
    };

    @action
    departureAddress = () => {
        const _this = this
        new window.daum.Postcode({
            oncomplete(data) {

                const roadAddr = data.roadAddress; 

                _this.addressState.departure.road = roadAddr;
            }
        })
    }
}