在react中使用react-mobx的情況下,數據已經被action 改變了,但是視圖層 沒有隨之改變
如果mobx的版本大於6
"mobx": "^6.3.2",
"mobx-react": "^7.2.0"
切記添加 makeObservable 初始化項目
import { observable, action, computed, makeObservable } from "mobx";
export class AuthStore {
@observable name = 'wangkai000';
@observable sex = '男';
@observable userObj = {
name: 'wangkai000',
age: 233,
token: '12345689'
}
constructor() {
// makeObservable 在mobx6 版本之後 比添加項
makeObservable(this);
}
@action.bound
setName(v) {
console.log('觸發action');
this.name = v;
}
@computed get titleName(){
return this.name+'___111';
}
}