【问题标题】:Sanity posts display all categories, not just ones associated with post理智帖子显示所有类别,而不仅仅是与帖子相关的类别
【发布时间】:2022-01-08 19:28:28
【问题描述】:

我正在使用 React 和 Sanity 创建博客,我想在每个帖子下方显示与每个帖子相关的类别。我能够显示类别,但它显示所有类别,而不仅仅是与每个特定帖子相关联的类别。我怎样才能使每个帖子只显示与其关联的类别?

这是我的代码:

import React, { useEffect, useState } from 'react'
import client from '../client'
import BlockContent from '@sanity/block-content-to-react'
import { Link } from 'react-router-dom'

function Main() {
    const [posts, setPosts] = useState([])

    useEffect(() => {
        client.fetch(
            `*[_type == "post"] {
                title,
                slug,
                body,
                author,
                mainImage {
                    asset -> {
                        _id,
                        url
                    },
                    alt
                },
                publishedAt,
                "categories": categories[]->title
            }`
        ).then((data) => setPosts(data))
         .catch(console.error)
    }, [])

    return (
        <div className='grid lg:grid-cols-3 md:grid-cols-2 gap-8 m-4 '>
            <div>
                {posts.slice(0, 1).map((p, i) => (
                    <Link to = {`/blog/${p.slug.current}`} className=''>
                        <article key = {p.slug.current} className=''>
                            <img src = {p.mainImage.asset.url} alt = {p.title} className='' />
                            <div>
                                <p className='font-bold text-xl text-secondary'>{p.title}</p>
                                <div className=''>
                                    <p className='text-sm'>By Brandon Pyle | {new Date(p.publishedAt).toLocaleDateString()}</p>
                                    {posts.map((c, i) => (
                                        <p className='inline'>{c.categories}, </p>
                                    ))}
                                </div>
                            </div>
                        </article>
                    </Link>
                ))}
            </div>

            <div className='my-[-16px]'>
                {posts.slice(1, 4).map((p, i) => (
                    <Link to = {`/blog/${p.slug.current}`} className='col-start-2'>
                        <article key = {p.slug.current} className='flex my-4'>
                            <img src = {p.mainImage.asset.url} alt = {p.title} className='w-auto h-auto max-h-[80px]' />
                            <div>
                                <p className='font-bold text-xl text-secondary'>{p.title}</p>
                                <p className='text-sm'>By Brandon Pyle | {new Date(p.publishedAt).toLocaleDateString()}</p>
                                <div>
                                    {posts.map((c, i) => (
                                        <p className='inline'>{c.categories}, </p>
                                    ))}
                                </div>
                            </div>
                        </article>
                    </Link>
                ))}
            </div>
        </div>
    )
}

export default Main

【问题讨论】:

  • 您的查询看起来不错。但是您在帖子的.map 中再次映射帖子。如果您只想要此帖子类别标题,我希望您在内部地图中执行p.categories.map

标签: reactjs tailwind-css sanity groq


【解决方案1】:

感谢@cfm,我找到了解决方案:

您的查询看起来不错。但是您在.map 中再次映射帖子 的帖子。我希望你在内部地图中做p.categories.map, 如果你只想要这个帖子类别标题。

按照他的建议,我只是将posts.map 切换为p.categories.map,这就解决了问题!这是完整的固定代码:

import React, { useEffect, useState } from 'react'
import client from '../client'
import BlockContent from '@sanity/block-content-to-react'
import { Link } from 'react-router-dom'

function Main() {
    const [posts, setPosts] = useState([])

    useEffect(() => {
        client.fetch(
            `*[_type == "post"] {
                title,
                slug,
                body,
                author,
                mainImage {
                    asset -> {
                        _id,
                        url
                    },
                    alt
                },
                publishedAt,
                "categories": categories[]->title
            }`
        ).then((data) => setPosts(data))
         .catch(console.error)
    }, [])

    return (
        <div className='grid lg:grid-cols-3 md:grid-cols-2 gap-8 m-4 '>
            <div>
                {posts.slice(0, 1).map((p, i) => (
                    <Link to = {`/blog/${p.slug.current}`} className=''>
                        <article key = {p.slug.current} className=''>
                            <img src = {p.mainImage.asset.url} alt = {p.title} className='' />
                            <div>
                                <p className='font-bold text-xl text-secondary'>{p.title}</p>
                                <div className=''>
                                    <p className='text-sm'>By Brandon Pyle | {new Date(p.publishedAt).toLocaleDateString()}</p>
                                    {p.categories.map((c, i) => (
                                        <p className='inline'>{c}, </p>
                                    ))}
                                </div>
                            </div>
                        </article>
                    </Link>
                ))}
            </div>

            <div className='my-[-16px]'>
                {posts.slice(1, 4).map((p, i) => (
                    <Link to = {`/blog/${p.slug.current}`} className='col-start-2'>
                        <article key = {p.slug.current} className='flex my-4'>
                            <img src = {p.mainImage.asset.url} alt = {p.title} className='w-auto h-auto max-h-[80px]' />
                            <div>
                                <p className='font-bold text-xl text-secondary'>{p.title}</p>
                                <p className='text-sm'>By Brandon Pyle | {new Date(p.publishedAt).toLocaleDateString()}</p>
                                <div>
                                    {p.categories.map((c, i) => (
                                        <p className='inline'>{c}, </p>
                                    ))}
                                </div>
                            </div>
                        </article>
                    </Link>
                ))}
            </div>
        </div>
    )
}

export default Main

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多