【问题标题】:How can I render Reference fields from Contentful in React?如何在 React 中从 Contentful 渲染参考字段?
【发布时间】:2023-03-24 18:50:01
【问题描述】:

如何呈现来自 Contentful 的 References 字段类型的数据?我可以在 graphql 中查询数据,甚至可以使用它来过滤我网站上的帖子;

query TestQuery {
  allContentfulPost {
    edges {
      node {
        categories {
          title
          slug
        }
      }
    }
  }
}

但是当我尝试渲染它以显示在页面中时,它会显示空白 div。但是,我能够呈现所有其他字段类型,只有 References 字段不起作用。

在 Contentful 中引用字段数据:

在 graphql 中获取的参考字段数据:

简单的测试代码:

import React from "react"
import { useStaticQuery, graphql } from "gatsby"

import Layout from "../components/Layout.js"

const TestPage = () => {
  const data = useStaticQuery(graphql`
    query TestQuery {
      allContentfulPost {
        edges {
          node {
            categories {
              title
              slug
            }
          }
        }
      }
    }
  `)

  return (
    <Layout>
      {data.allContentfulPost.edges.map(({ node }) => {
        return (
          <div key={node.slug}>
            {node.categories.title}
            Reference fields should be here but their not ????
          </div>
        )
      })}
    </Layout>
  )
}

export default TestPage

参考字段数据未在 React 中呈现:

//

更新:

我已经按照下面 Ferran 的建议对 GraphQL 片段进行了一些试验和错误,我能够查看 localhost:8000/___graphql 中的数据;

query TestQuery {
      allContentfulPost {
        edges {
          node {
            categories {
              ... on ContentfulCategory {
                title
                slug
              }
            }
          }
        }
      }
    }

但是,呈现的 div 仍然是空的。我是否需要在返回函数中更改数据源“{node.categories.title}”的路径才能使其工作?我是否可能需要在“节点”和“类别”之间添加一些内容以引用在 graphql 查询中添加的“... on ContentfulCategory”?如果是这样,我应该怎么写?我试过“{node.oncontentfulcategory.categories.title}”,但没有奏效。再次感谢

import React from "react"
import { useStaticQuery, graphql } from "gatsby"

import Layout from "../components/Layout.js"

const TestPage = () => {
  const data = useStaticQuery(graphql`
    query TestQuery {
      allContentfulPost {
        edges {
          node {
            categories {
              ... on ContentfulCategory {
                title
                slug
              }
            }
          }
        }
      }
    }
  `)

  return (
    <Layout>
      {data.allContentfulPost.edges.map(({ node }) => {
        return (
          <div key={node.slug}>
            {node.categories.title}
            What path should I now use for the data source in the line above?
          </div>
        )
      })}
    </Layout>
  )
}

export default TestPage

【问题讨论】:

    标签: reactjs graphql gatsby contentful


    【解决方案1】:

    您需要在查询中添加 GraphQL 片段以访问参考数据。你在Gatsby docs (gatsby-source-contentful) 或其他GitHub threads 中有一个糟糕的例子。

    该插件将推断您的架构引用并创建适当的节点,允许您在查询中使用它们来检索所需的数据。您应该在 localhost:8000/___graphql 中进行一些试验和错误,以确保您的模型正常工作,否则很难猜测查询应该是什么样子,但应该是这样的:

      const data = useStaticQuery(graphql`
        query TestQuery {
          allContentfulPost {
            edges {
              node {
                categories {
                     title
                     slug
                     __typename
                   ...on contentfulCategories{
                     #your category fields
                  }
                }
              }
            }
          }
        }
      `)
    

    更多参考资料/文章:

    【讨论】:

    • 谢谢费兰!我还是被困住了。请在下面查看我的答案。
    • 请编辑您的帖子,而不是添加新答案。这比那更容易。字段名称和嵌套属性由 localhost:8000/___graphql 提供,要访问数据,您只需将其打印到 console.log 中,然后查看其中的内容。
    • 很抱歉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 2020-03-17
    • 2016-11-23
    • 2021-10-16
    • 1970-01-01
    • 2017-08-12
    相关资源
    最近更新 更多