【发布时间】:2021-12-16 13:22:12
【问题描述】:
但是,它们只返回一个数组!
我的变量(我的 bdd Firebase 的内容)包含以下内容:
Array [ {…}, {…} ]
0: Object { title: "boissonstitle", category: "boissons", describe: "boissonsdesc", … }
1: Object { title: "Oeuf en neige", category: "Desserts", describe: "Les œufs à la neige sont un entremets ou dessert de la cuisine française composé de blancs d'œufs sucrés, fouettés, pochés ou cuits au bain-marie, accompagnés d'une crème anglaise et généralement nappés de caramel", … }
2: Object { category: "Entrées", describe: "Entréesdesc", title: "Entréestitle", … }
3: Object { title: "repastitle", category: "repas", describe: "repasdesc", … }
length: 4
套间,enfin,然后我做这个:
return (
<div className="container-fluid css-selector m-0 p-0" style={{ height: '100vh' }}>
<div>
{products ? (
products.map((item) => {
return (
<div>
{/* Liste d'items à afficher */}
<FlatList data={item} plainList={true} list={item} />
<h2>{item.title}</h2>
<h2>{item.describe}</h2>
</div>
);
})
) : (
<h1>Hi</h1>
)}
</div>
</div>
);
};
但它们只返回一个对象,为什么它们不迭代所有对象?
这是我的全部代码:
import React, { useState, useEffect } from 'react';
import '../App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import { collection, query, where, getDocs } from 'firebase/firestore';
import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
import FlatList from 'flatlist-react';
import Empty from './Empty';
const firebaseApp = initializeApp({
// Myconfig
});
const db = getFirestore();
const Allproducts = () => {
const [ products, setProducts ] = useState(); // description
const [ title, setTitle ] = useState();
// console.log(products);
const getUser = async () => {
const q = query(collection(db, 'User'));
const Array = [];
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
Array.push(doc.data());
setProducts(Array);
});
};
console.log(products);
useEffect(() => {
getUser();
}, []);
return (
<div className="container-fluid css-selector m-0 p-0" style={{ height: '100vh' }}>
<div>
{products ? (
products.map((item) => {
return (
<div>
{/* Liste d'items à afficher */}
<FlatList data={item} plainList={true} list={item} />
<h2>{item.title}</h2>
<h2>{item.describe}</h2>
</div>
);
})
) : (
<h1>Hi</h1>
)}
</div>
</div>
);
};
export default Allproducts;
【问题讨论】:
-
根据 FlatList 文档,您需要传递
renderItem属性,该属性是呈现单个项目的函数:renderItem={your_func}
标签: reactjs javascript-objects array.prototype.map