网格 /文档数组未加载时首次单击
我试图将一系列文档加载到Firebase Firestore的网格(react.js)中,但并未在“导航”按钮的第一个点击中显示。 即使我添加了一个加载组件,也可以在网格之前显示 - 它仍然不起作用
getAuthor func
const getAuthor = async (uid) => {
const author = await getDoc(doc(getFirestore(app), 'StripeCustomers', uid));
if (author.exists()) {
return author.data();
}
return null;
}
getGrid func
const getGrid = async (type) => {
let gridData = [];
const grid = await getDocs(collection(getFirestore(app), type));
grid?.docs.map(item => {
getAuthor(item.data().Author.id).then(author => {
gridData.push({
title: item.data().title,
link: "/materials/type=" + type + "/uuid=" + item.id,
thumbnail: "#",
type: type,
authors: [{
name: author.FirstName + " " + author.LastName,
imageUrl: author.profileUrl,
profileUrl: "/user/id=" + author.id
}]
});
});
});
return gridData;
}
partial:
const Material = ({ info }) => {
return (
<Link to={info.link} className="flex flex-col dark:black hover:border hover:bg-gray-100 dark:hover:bg-slate-900 hover:border-gray-200 dark:hover:border-slate-800 rounded-2xl cursor-pointer overflow-hidden box-border">
<img src={info.thumbnail} className="thumbnail aspect-video rounded-2xl" />
<div className="flex items-center p-3 space-x-4">
{/* <Link to='#'><img src={info.authors[0].imageUrl} className="w-12 block aspect-square rounded-full" /></Link> */}
<div className="flex flex-col">
<h3 className="text-xl font-bold">{info.title}</h3>
{info.authors.map((author, index) => (<p key={index} className="text-sm text-gray-500">{author.name}</p>))}
</div>
</div>
</Link>
);
}
main:
const Materials = ({ groupBy }) => {
const [grid, setGrid] = useState([]);
useEffect(() => {
getGrid(groupBy).then(gridn => {
setGrid(gridn);
});
}, []);
return (
<div className="w-full flex flex-col justify-content items-center m-auto">
<div className="materials w-11/12 lg:p-5 pt-5 grid 2xl:grid-cols-4 xl:grid-cols-3 lg:grid-cols-2 gap-4 gap-y-6">
{grid && grid?.map((i) => (<Material key={i} info={i} />))}
</div>
</div>
)
}
I am trying to load an array of documents into a grid (in React.js) from Firebase FireStore, but it isn't displaying on the first tap of the navigation button.
Even if I add a loading component, that displays before the grid - it still doesn't work
getAuthor func
const getAuthor = async (uid) => {
const author = await getDoc(doc(getFirestore(app), 'StripeCustomers', uid));
if (author.exists()) {
return author.data();
}
return null;
}
getGrid func
const getGrid = async (type) => {
let gridData = [];
const grid = await getDocs(collection(getFirestore(app), type));
grid?.docs.map(item => {
getAuthor(item.data().Author.id).then(author => {
gridData.push({
title: item.data().title,
link: "/materials/type=" + type + "/uuid=" + item.id,
thumbnail: "#",
type: type,
authors: [{
name: author.FirstName + " " + author.LastName,
imageUrl: author.profileUrl,
profileUrl: "/user/id=" + author.id
}]
});
});
});
return gridData;
}
Partial:
const Material = ({ info }) => {
return (
<Link to={info.link} className="flex flex-col dark:black hover:border hover:bg-gray-100 dark:hover:bg-slate-900 hover:border-gray-200 dark:hover:border-slate-800 rounded-2xl cursor-pointer overflow-hidden box-border">
<img src={info.thumbnail} className="thumbnail aspect-video rounded-2xl" />
<div className="flex items-center p-3 space-x-4">
{/* <Link to='#'><img src={info.authors[0].imageUrl} className="w-12 block aspect-square rounded-full" /></Link> */}
<div className="flex flex-col">
<h3 className="text-xl font-bold">{info.title}</h3>
{info.authors.map((author, index) => (<p key={index} className="text-sm text-gray-500">{author.name}</p>))}
</div>
</div>
</Link>
);
}
Main:
const Materials = ({ groupBy }) => {
const [grid, setGrid] = useState([]);
useEffect(() => {
getGrid(groupBy).then(gridn => {
setGrid(gridn);
});
}, []);
return (
<div className="w-full flex flex-col justify-content items-center m-auto">
<div className="materials w-11/12 lg:p-5 pt-5 grid 2xl:grid-cols-4 xl:grid-cols-3 lg:grid-cols-2 gap-4 gap-y-6">
{grid && grid?.map((i) => (<Material key={i} info={i} />))}
</div>
</div>
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在调试器中检查(或添加一些记录),您会看到
返回GridData
在您的任何griddata.push({...})之前运行之前运行发生了,您无法返回尚未加载的东西。
一个简单的解决方案是使用
Promise.All
,例如:If you check in a debugger (or add some logging), you'll see that
return gridData
runs before any of yourgridData.push({ ... })
calls happen, and you can't return something that hasn't loaded yet.A simple solution is to use
Promise.all
, like this: