React native Firebase和React本机Flatlist之间的JSON格式
这就是Firebase实时数据库中存储的数据React native Firebase和React本机Flatlist之间的JSON格式,react-native,react-native-flatlist,react-native-firebase,React Native,React Native Flatlist,React Native Firebase,这就是Firebase实时数据库中存储的数据 "Users" : { "Joe" : { "name" : "xxx", "email" : "xxx" }, "Matt" : { "name" : "xxx", "email" : "xxx" } } 这是React原生平面列表中的数据所需 Users : [ { id : "Joe" name : "xxx", email : "xxx", }, { i
"Users" : {
"Joe" : {
"name" : "xxx",
"email" : "xxx"
},
"Matt" : {
"name" : "xxx",
"email" : "xxx"
}
}
这是React原生平面列表中的数据所需
Users : [
{
id : "Joe"
name : "xxx",
email : "xxx",
},
{
id : "Matt"
name : "xxx",
email : "xxx",
}
]
在componentDidMount()中的某个地方
在渲染平面列表中:
<FlatList
extraData={this.props}
data={this.props.users}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => (
...
index.toString()}
renderItem={({item})=>(
...
如何使用firebase.database().ref()
并返回flatlist中所需的数据?您可以尝试以下方法:
我的构造函数中的第一个
constructor(props) {
super(props);
this.state = {
arrData:[]
};
}
然后从firebase获取数据
var ref = firebase.database().ref("Users"); //Here assuming 'Users' as main table of contents
ref.once('value').then(snapshot => {
// console.log(snapshot.val());
// get children as an array
var items = [];
snapshot.forEach((child) => {
items.push({
id: child.val().id,
name: child.val().name,
email: child.val().email,
phone: child.val().phone,
status: child.val().status,
user_type: child.val().user_type
});
});
this.setState({ arrData: items});
});
现在您可以在FlatList
或ListView
中填充arrData
希望对您有所帮助!这里我得到了用户在对象中发布的问题数组,然后我创建了一个数组,然后将该数组分配给变量。
componentDidMount(){
让user=firebase.auth().currentUser;
让uid=user.uid;
firebase.database().ref('Problems').orderByChild('userid').equalTo(uid)
.once('值')
。然后(快照=>{
//console.log(snapshot.val());
var项目=[];
snapshot.forEach((子项)=>{
推({
id:child.key,
problemDescription:child.val().problemDescription,
problemTitle:child.val().problemTitle,
problemstatus:child.val().problemstatus,
timeanddate:child.val().timeanddate,
});
});
控制台日志(项目);
this.setState({problmes:items});
this.setState({isloading:false});
})
.catch(错误=>console.log(错误));
}
数据是否应该一个或多个对象数组扫描您是否共享您尝试过的一些代码?@iRiziya我只是更新问题OK检查我的答案OnceWalcome to Stackoverflow!请编辑您的答案并提供解释。Stackoverflow不喜欢只使用代码的答案,它被放在一个低质量的post队列中,以查看是否删除。若要防止删除,请说明它的作用。请参阅“”。虽然这在技术上可能是正确的,但它并没有解释为什么它可以解决问题,或者它应该是选定的答案。除了帮助解决问题之外,我们还应该进行教育。
var ref = firebase.database().ref("Users"); //Here assuming 'Users' as main table of contents
ref.once('value').then(snapshot => {
// console.log(snapshot.val());
// get children as an array
var items = [];
snapshot.forEach((child) => {
items.push({
id: child.val().id,
name: child.val().name,
email: child.val().email,
phone: child.val().phone,
status: child.val().status,
user_type: child.val().user_type
});
});
this.setState({ arrData: items});
});