【问题标题】:How to display data from firestore using react native如何使用本机反应显示来自 Firestore 的数据
【发布时间】:2020-07-10 22:57:27
【问题描述】:

我正在尝试在主页上显示来自 firestore 的数据,但我似乎无法正常工作。我想显示集合中的所有数据。该集合包括名称、ID、位置等...

    import Firebase from "./lib/firebase";
import { useEffect, useState } from "react";
import { SnapshotViewIOS } from "react-native";
// export async function getRestaurants(restaurantsRetrieved) {
//   var restaurantList = [];

//   var snapshot = await firebase.firestore().collection("Restaurants").get();

//   snapshot.forEach((doc) => {
//     restaurantList.push(doc.data());
//   });

//   restauantsRetrieved(restaurantList);
// }
export default () => {
  const [restaurantsList, setRestaurantsList] = useState([]); //Initialise restaurant list with setter
  const [errorMessage, setErrorMessage] = useState("");

  const getRestaurants = async () => {
    try {
      const list = [];
      var snapshot = await Firebase.firestore().collection("Restaurants").get();
      console.log("Here");
      snapshot.forEach((doc) => {
        list.push(doc.data());
      });
      setRestaurantsList(list);
    } catch (e) {
      setErrorMessage(
        "There's nae bleeding restaurants, I told you to upload them!"
      );
    }
  };

  //Call when component is rendered
  useEffect(() => {
    getRestaurants();
  }, []);

  return (

  <View style={tailwind('py-10 px-5')}>
    <Text style={tailwind('text-4xl font-bold')}>
      {restaurantsList}
    </Text>
};

【问题讨论】:

    标签: reactjs firebase react-native google-cloud-firestore


    【解决方案1】:

    状态没有更新,因为它只是参考了列表。所以你需要更新下面的类似代码

    .....
    
    setRestaurantsList([...list]);
    
    ...... 
    
    

    【讨论】:

    • 嗨@Ferin 我收到此错误然后“错误:对象作为 React 子项无效(找到:带有键的对象 {地址,经度,ID,图像(url),名称,价格,电话, 纬度})。如果您打算渲染一组子项,请改用数组。”
    • {restaurantsList} 你不能在 标签内打印这样的数据。
    • 它的工作现在只需将返回放入一个 FlatList !谢谢
    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 2019-01-23
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多