Reputation: 1238
I want to change state with a function that is at same store of state.
For example, I have some code like below.
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;
})
}
I can make roadAddr by daum post api. and there is one more function in the method called oncomplete.
So when I run 'departureAddress' function that says
TypeError:this.addressState is undefined.
Maybe this error comes from the position error of this.addressState that is inside on function of one method.
Could you help me this problem?
PS) Must I use action or computed of mobx in this case to change state?
Upvotes: 0
Views: 54
Reputation: 1746
what about try this?
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;
}
})
}
}
Upvotes: 1