简体   繁体   English

如何从 Firestore 渲染数据以响应本机组件?

[英]How Can I Render Data From Firestore To React Native Component?

I have been trying to render information from my firebase to a react native component.我一直在尝试将我的 firebase 中的信息渲染到反应原生组件。 I started by console logging what I have done, the data is being fetched completely fine:我首先通过控制台记录我所做的事情,数据被完全提取:

displayAllPlayers(){
dbh.collection('Players').get()
.then(querySnapshot => {
    querySnapshot.forEach(doc => {
        console.log(doc.data().First, doc.data().Last)
    })
})}

I then tried to add this information to my component as follows:然后我尝试将此信息添加到我的组件中,如下所示:

displayAllPlayers(){
dbh.collection('Players').get()
.then(querySnapshot => {
    querySnapshot.forEach(doc => {
        <Player key={doc.data().First} fName={doc.data().First} lName={doc.data().Last} />
    })
})
}
render() { 
    const myPlayers = this.displayAllPlayers()
} 
return(
    {myPlayers}
)
  • You should return the JSX inside the render function.您应该在render function 中返回 JSX。

  • displayAllPlayers isn't returning anything. displayAllPlayers没有返回任何东西。

  • In this snippet在这个片段中

     querySnapshot.forEach(doc => { <Player key={doc.data().First} fName={doc.data().First} lName={doc.data().Last} /> })

    you're not returning anything inside the callback passed to forEach even if you do, it doesn't work because forEach doesn't return an array.即使你这样做了,你也不会在传递给forEach的回调中返回任何内容,它不起作用,因为forEach不返回数组。 You can use map here.您可以在此处使用map

  • Maintain a state in the component and update it once you get the data.在组件中维护一个 state 并在获得数据后对其进行更新。 Use this state for rendering the array of JSX elements.使用这个 state 来渲染 JSX 元素的数组。

It's always suggested to create a different helpers file.总是建议创建一个不同的帮助文件。

Create a firebase-helpers.js file which has an function to convert snapshot to array.创建一个包含 function 的 firebase firebase-helpers.js文件以将快照转换为数组。

// FILENAME - firebase-helpers.js
export const snapshotToArray = (snapshot) => {
  let returnArr = [];

  snapshot.forEach((childSnapshot) => {
    let item = childSnapshot.data();

    returnArr.push(item);
  });

  return returnArr;
};

Now in your screen, import this file现在在您的屏幕中,导入此文件

import { snapshotToArray } from "../helpers/firebaseHelpers";

Then, convert snapshot of Players to array然后,将 Players 的快照转换为数组

const playersSnapshot = dbh.collection('Players').get();
const playersArray = snapshotToArray(playersSnapshot);
this.setState({ players : playersArray });

Now in state you have an array players.现在在 state 你有一个阵列球员。 To display content of Players, you can use in your render function as -要显示播放器的内容,您可以在渲染中使用 function 作为 -

<FlatList
   data={this.state.players}
   renderItem={({ item }, index) => this.playerDisplay(item, index)}
   keyExtractor={(item, index) => index.toString()}
/>

You can then have a function to return details of players as -然后,您可以使用 function 将玩家的详细信息返回为 -

playerDisplay = (item, index) => {
  return(
    <View>
          <Text>
                Player {index} - {item.First} {item.Last}
          </Text>
    </View>
  );
}

I hope it works fine.我希望它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM