直接上代碼
import React,{Component} from 'react';
let instance = null;
export default class AppDataManager extends Component {
constructor(props){
super(props);
if(!instance){
instance = this;
console.warn('creat ....');
//注意1:一定要在if內(nèi)部肚医,這里只能執(zhí)行一次
this.state = {
userInfo:{},
}
}
return instance;
}
static shareInstance(){
let singleton = new AppDataManager();
return singleton;
}
setUserInfo(info){
//注意2:這里不能用setstate
this.state.userInfo = info;
}
getUserInfo(){
return this.state.userInfo;
}
}
疑問分尸,注意2哪里不知道為啥不能用setState來賦值乍楚。求告知!4猿族壳!