Firebase Can';t在mobx存储中重写可观察变量
这是我的Mobx商店:Firebase Can';t在mobx存储中重写可观察变量,firebase,react-native,firebase-realtime-database,mobx,mobx-react,Firebase,React Native,Firebase Realtime Database,Mobx,Mobx React,这是我的Mobx商店: 从'mobx'导入{observable}; 从“../Firebase”导入fb; 从“../User”导入用户; 导出默认类配置文件{ @可观测剖面={}; @可观察uid=null; /** *@return{void} */ 构造函数(用户){ this.user=用户; fb.firebase.auth().onAuthStateChanged(()=>{ this.uid=user.currentUser.uid; fb.profiles.child(thi
从'mobx'导入{observable};
从“../Firebase”导入fb;
从“../User”导入用户;
导出默认类配置文件{
@可观测剖面={};
@可观察uid=null;
/**
*@return{void}
*/
构造函数(用户){
this.user=用户;
fb.firebase.auth().onAuthStateChanged(()=>{
this.uid=user.currentUser.uid;
fb.profiles.child(this.uid).on('value',(快照)=>{
//不管我用这个做什么。个人资料。。。
this.profile=snapshot.val();
});
});
}
}
为了正确更新对象,您应该使用可观察的()和对象。分配来定义配置文件
以下是几个(未经测试的)示例:
@observable profile = observable({});
.
.
.
this.profile = Object.assign({}, this.profile, snapshot.val());
如果Object.assign
未触发更新,则需要使用extendObservable()
():
另一种选择是对更复杂的结构使用observable.map
():
@observable profile = observable.map({ profileData: {} });
.
.
.
const currentProfile = this.profile.get('profileData');
this.profile.set('profileData', Object.assign({}, currentProfile, snapshot.val()));
所以用问题解决我的问题是不可能的,最重要的部分是缺失
组件中有一个组件willmount。删除后,它解决了我的问题。仍然不走运。如果我使用我从组件调用的简单方法将概要文件从存储区打印到控制台,那么概要文件日期将是控制台中的更新数据,但组件中的对象为空。您有过这样的经历吗?
@observable profile = observable.map({ profileData: {} });
.
.
.
const currentProfile = this.profile.get('profileData');
this.profile.set('profileData', Object.assign({}, currentProfile, snapshot.val()));