[英]How to convert or map a Firebase Snapshot child into a custom object in React Native
I need to get data from Firebase Realtime & display within a FlatList.我需要从 Firebase 实时获取数据并在 FlatList 中显示。
What's the best way to capture the child value & map or convert it into my custom object?捕获子值 & map 或将其转换为我的自定义值 object 的最佳方法是什么?
Example:例子:
class ObjectModel {
constructor(id, name, age) {
this.id = id;
this.name = name;
this.age = age;
}
}
export default new ObjectModel();
// on my Screen.js file, getting the data...
useEffect(() => {
reference
.on('value', snapshot => {
let data = [];
snapshot.forEach((child) => {
// I need to make the child.val() to be an ObjectModel
data.push(child.val());
})
setArr(data);
});
});
Try spreading the val like this and then pushing it to your array:尝试像这样传播 val,然后将其推送到您的数组:
let newObjModel = new ObjectModel ({...child.val()})
is this is what you were looking for?这是你要找的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.