英文:
Posts in my .map() function are not displaying on browser
问题
在我的代码中,我已经反复运行了很多次,但是我无法找出问题所在。我正在按照一个使用 Next.js 13.4 和 Sanity.io 的博客构建教程进行操作。
在我的 BlogList
组件中,通过 console.log
打印出的 posts
在终端中显示得很好,但是当我将这些帖子映射到页面上时,页面上什么都不显示。
没有任何错误的迹象。如果您能再仔细查看一下代码,我会很感激。您可能会发现我忽略的东西。
page.tsx
- 博客主页
import { draftMode } from "next/headers";
import { groq } from "next-sanity";
import { client } from "@/lib/sanity.client";
import BlogList from "@/components/blog/BlogList.tsx";
import BlogHeader from "@/components/blog/BlogHeader";
import PreviewSuspense from "@/components/studio/PreviewSuspense";
import PreviewBlogList from "@/components/studio/PreviewBlogList";
const query = groq`
*[_type=='post'] {
...,
_id,
_createdAt,
author->,
"slug": slug.current
} | order(_createdAt desc)
`;
export default async function page() {
const { isEnabled } = draftMode();
if (isEnabled) {
return (
<PreviewSuspense
fallback={
<div role="status">
<p className="text-center text-lg animate-pulse text-blue-800">
Loading Draft Data
</p>
</div>
}
>
<PreviewBlogList query={query} />
</PreviewSuspense>
);
}
const posts = await client.fetch<Post[]>(query);
// console.log(posts);
return (
<>
<BlogHeader />
<section
id="blog"
className="w-full xl:pt-10 xl:pb-24 pb-12 p-4 flex flex-col gap-10 xl:gap-0 align-middle items-center border-b-[1px] border-b-gray-300 dark:border-b-gray-500"
>
<BlogList posts={posts} />
</section>
</>
);
}
BlogList.tsx
- 博客列表组件 - 特别是在 .map()
函数中的内容没有显示在页面上。然而,在 .map()
函数之外的任何内容都会显示出来。
import Image from "next/image";
import { urlForImage } from "../../../sanity/lib/image";
interface Props {
posts: Post[];
}
export default function BlogList({ posts }: Props) {
console.log(posts);
return (
<>
<div className="grid grid-cols-1 gap-8 mt-8 md:mt-16 md:grid-cols-2 xl:grid-cols-3">
{posts.map((post) => {
return (
<div key={post._id} className="group cursor-pointer">
<div className="relative w-full h-64 lg:h-80 group-hover:scale-105 drop-shadow-xl">
<Image
className="object-cover object-center rounded-lg"
src={urlForImage(post.mainImage).url()}
alt={post.author.name}
fill
/>
<h1>hello there</h1>
<div className="absolute bottom-0 flex p-3 bg-white dark:bg-gray-900 ">
<Image
className="rounded-full"
src={urlForImage(post.author.image).url()}
alt={post.author.name}
width={10}
height={10}
/>
<div className="mx-4">
<h1 className="text-sm text-gray-700 dark:text-gray-200">
{post.author.name}
</h1>
<p className="text-sm text-gray-500 dark:text-gray-400">
{new Date(post._createdAt).toLocaleDateString("en-US", {
day: "numeric",
month: "numeric",
year: "numeric",
})}
</p>
</div>
</div>
</div>
<h1 className="mt-6 text-xl font-semibold text-gray-800 dark:text-white">
{post.title}
</h1>
<hr className="w-32 my-6 text-blue-500" />
<p className="text-sm text-gray-500 dark:text-gray-400">
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Blanditiis fugit dolorum amet dolores praesentium, alias nam?
Tempore
</p>
<a
href="#"
className="inline-block mt-4 text-blue-500 underline hover:text-blue-400"
>
Read more
</a>
</div>
);
})}
</div>
</>
);
}
这是我对您的问题的翻译,如果您需要进一步的帮助,请随时告诉我。
英文:
I have run through my code countless times but I can't make out the problem. I am following a blog build tutotial with Nextjs 13.4 with Sanity.io.
In my BlogList
component, a console log
of my posts
reveals all my post in the terminal alright, but when I map the post into the UI, nothing shows up on my page.
There is no indication of an error anywhere. I would love it if you could take a second look at the code. You might find something I missed.
page.tsx
- Blog Home Page
import { draftMode } from "next/headers";
import { groq } from "next-sanity";
import { client } from "@/lib/sanity.client";
import BlogList from "@/components/blog/BlogList.tsx";
import BlogHeader from "@/components/blog/BlogHeader";
import PreviewSuspense from "@/components/studio/PreviewSuspense";
import PreviewBlogList from "@/components/studio/PreviewBlogList";
const query = groq`
*[_type=='post'] {
...,
_id,
_createdAt,
author->,
"slug": slug.current
} | order(_createdAt desc)
`;
export default async function page() {
const { isEnabled } = draftMode();
if (isEnabled) {
return (
<PreviewSuspense
fallback={
<div role="status">
<p className="text-center text-lg animate-pulse text-blue-800">
Loading Draft Data
</p>
</div>
}
>
<PreviewBlogList query={query} />
</PreviewSuspense>
);
}
const posts = await client.fetch<Post[]>(query);
// console.log(posts);
return (
<>
<BlogHeader />
<section
id="blog"
className="w-full xl:pt-10 xl:pb-24 pb-12 p-4 flex flex-col gap-10 xl:gap-0 align-middle items-center border-b-[1px] border-b-gray-300 dark:border-b-gray-500"
>
<BlogList posts={posts} />
</section>
</>
);
}
BlogList.tsx
- Blog List Component - Nothing on this page especially content in the .map() function shows up on the page. Any Content outside of the .map() function shows up though.
import Image from "next/image";
import { urlForImage } from "../../../sanity/lib/image";
interface Props {
posts: Post[];
}
export default function BlogList({ posts }: Props) {
console.log(posts);
return (
<>
<div className="grid grid-cols-1 gap-8 mt-8 md:mt-16 md:grid-cols-2 xl:grid-cols-3">
{posts.map((post) => {
<div key={post._id} className="group cursor-pointer">
<div className="relative w-full h-64 lg:h-80 group-hover:scale-105 drop-shadow-xl">
<Image
className="object-cover object-center rounded-lg"
src={urlForImage(post.mainImage).url()}
alt={post.author.name}
fill
/>
<h1>hello there</h1>
<div className="absolute bottom-0 flex p-3 bg-white dark:bg-gray-900 ">
<Image
className="rounded-full"
src={urlForImage(post.author.image).url()}
alt={post.author.name}
width={10}
height={10}
/>
<div className="mx-4">
<h1 className="text-sm text-gray-700 dark:text-gray-200">
{post.author.name}
</h1>
<p className="text-sm text-gray-500 dark:text-gray-400">
{new Date(post._createdAt).toLocaleDateString("en-US", {
day: "numeric",
month: "numeric",
year: "numeric",
})}
</p>
</div>
</div>
</div>
<h1 className="mt-6 text-xl font-semibold text-gray-800 dark:text-white">
{post.title}
</h1>
<hr className="w-32 my-6 text-blue-500" />
<p className="text-sm text-gray-500 dark:text-gray-400">
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Blanditiis fugit dolorum amet dolores praesentium, alias nam?
Tempore
</p>
<a
href="#"
className="inline-block mt-4 text-blue-500 underline hover:text-blue-400"
>
Read more
</a>
</div>;
})}
</div>
</>
);
}
Console Log for my Posts
[
{
slug: 'blog-development-with-next-13-4-and-sanity',
mainImage: { _type: 'image', alt: 'Cover Image', asset: [Object] },
publishedAt: '2023-06-14T11:15:00.000Z',
_type: 'post',
categories: [ [Object], [Object] ],
title: 'Blog Development with Next 13.4 and Sanity',
author: {
name: 'Edward Opoku-Agyemang',
_updatedAt: '2023-06-14T11:11:24Z',
slug: [Object],
image: [Object],
_createdAt: '2023-06-14T11:11:24Z',
_rev: 'sKRwurG6U83UyjXGn2pQeT',
_type: 'author',
bio: [Array],
_id: '46ec3787-817d-441f-be7b-eab31e9ed3db'
},
_id: '0fa6470e-f88f-46d7-a127-a932b0f148fb',
_updatedAt: '2023-06-14T11:15:49Z',
_createdAt: '2023-06-14T11:15:33Z',
_rev: 'TrPpVOhlpnRSDfzu76fyos',
body: [ [Object], [Object], [Object], [Object] ]
}
]
答案1
得分: 0
你在BlogList.tsx中的映射中没有返回任何内容。
{posts.map((post) => {
你只需要从映射的末尾删除 { 和 }。这将执行隐式返回并返回标记。
或者你需要这样做
{posts.map((post) => {
return (
)
}
英文:
You are not returning anything from the map in BlogList.tsx
{posts.map((post) => {
You just need to remove the { and the } from the end of the map. This will do an implicit return and return the markup.
Alternative you need to do
{posts.map((post) => {
return (<div></div>)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论