在我的react native应用程序中不会重新抛出数据

zvms9eto  于 2023-03-24  发布在  React
关注(0)|答案(1)|浏览(131)

我已经创建了一个简单的React Native应用程序与消防基地和数据rethrow是不工作.当运行应用程序的数据是可见的控制台上,但数据不能在移动的屏幕上查看.请帮助我解决这个问题,
这是我的App.js文件

import React from "react";
import { SafeAreaView, TextInput } from "react-native";
import { StyleSheet, Text, View, Pressable, FlatList } from "react-native";
import { useEffect, useState } from "react";
import { MaterialIcons } from "@expo/vector-icons";
import ShoppingItems from "./components/ShoppingItems";
import {app,db,getFirestore,collection,addDoc,getDocs,} from "./firebase/index";
import { ActivityIndicator } from "react-native";

export default function App() {
  
  const [title, setTitle] = useState("");
  const [shoppingList, setShoppingList] = useState([]);

  const addShopingItem = async () => {
    try {
      const docRef = await addDoc(collection(db, "shopping"), {
        title: title,
        isCheked: false,
      });
      console.log("Document written with ID: ", docRef.id);
      setTitle("");
    } catch (e) {
      console.error("Error adding document: ", e);
    }
  };

  const getShoppingItems = async () => {
    const querySnapshot = await getDocs(collection(db, "shopping"));
    querySnapshot.forEach((doc) => {
      console.log(doc.id, doc.data());
      setShoppingList({
        ...doc.data(),
        id: doc.id,
      });
    });
  };

  useEffect(() => {
    getShoppingItems();
  }, []);

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.header}>
        {/* header */}
        <Text style={styles.heading}>Sghopping Items</Text>
        {/* noofshoppng items */}
        <Text style={styles.noOfItems}>2</Text>
        {/* delete button */}
        <Pressable>
          <MaterialIcons
            style={styles.deleteIcon}
            name="delete"
            size={30}
            color="black"
          />
        </Pressable>
      </View>
      {/* flat List */}
      {shoppingList.length > 0 ? (
        <FlatList
          data={shoppingList}
          renderItem={({ item }) => <ShoppingItems title={item.title} />}
          keyExtrator={(item) => item.id}
        />
      ) : (
        <ActivityIndicator />
      )}

      {/* Text inpus */}
      <TextInput
        style={styles.input}
        placeholder="Enter Shoping Itms"
        value={title}
        onChangeText={(text) => setTitle(text)}
        onSubmitEditing={addShopingItem}
      />
    </SafeAreaView>
  );
}

这是我的ShppingItems.js文件

import { View, Text, StyleSheet } from "react-native";
    import React from "react";
    import { AntDesign, MaterialIcons } from "@expo/vector-icons";
    import { Pressable } from "react-native";
    
    const ShoppingItems = (props) => {
      return (
        <View style={styles.container}>
          <Pressable>
            <AntDesign name="checkcircleo" size={24} color="black" />
          </Pressable>
          <Text style={styles.title}>{props.title}</Text>
        </View>
      );
    };

export default ShoppingItems;

这是控制台enter image description here

cvxl0en2

cvxl0en21#

Flatlist接收数组作为数据。

<FlatList
  data={[{title: 'Title Text', key: 'item1'}]}
  ...
>

data:为了简单起见,data是一个普通数组。如果你想使用其他东西,比如不可变的列表,直接使用底层的VirtualizedList。[来源:https://reactnative.dev/docs/flatlist]
你用一个对象而不是一个数组来填充它:

setShoppingList({
        ...doc.data(),
        id: doc.id,
      });

正确的代码应该是:

const getShoppingItems = async () => {
  const querySnapshot = await getDocs(collection(db, "shopping"));
  const newShoppingList = [];
  querySnapshot.forEach((doc) => {
    console.log(doc.id, doc.data());
    newShoppingList.push({
      ...doc.data(),
      id: doc.id,
    });
  });
  setShoppingList(newShoppingList);
};

相关问题