简体   繁体   中英

Contentful render long text

I'm figuring out how to render standard long text , but having issues.

Please take a look at my query, that is working when ran in the graphQL playground.

{
  allContentfulArticle {
    nodes {
      title
      slug
      para {
        para
      }
    }
  }
}

Here is my article.js file

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

const Article = props => (
  <div>
    <header>
      <h1>{props.data.site.siteMetadata.title}</h1>
    </header>

    <main>
      <h1>{props.data.contentfulArticle.title}</h1>
    </main>

    <footer>
    <p>{props.data.site.siteMetadata.author}</p>
    </footer>
  </div>
)

export default Article

export const query = graphql`
  query Article($id: String) {
    site {
      siteMetadata {
        title
        description
        author
      }
    }
    contentfulArticle(id: { eq: $id }) {
      title
      subtitle
      slug
      para {
        para
      }
    }
  }
`

The strange thing is that I can render the title easily which is a simialir content type - just short.

I can see my title , slug and other metadata fields being displayed on localhost, but not the para paragraph.

在此处输入图像描述

You are not rendering your long text field despite your GraphQL query looks perfect. Just use:

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

const Article = props => (
  <div>
    <header>
      <h1>{props.data.site.siteMetadata.title}</h1>
    </header>

    <main>
      <h1>{props.data.contentfulArticle.title}</h1>
    </main>
    <p>Your long text is: {props.data.contentfulArticle.para.para}</p>
    <footer>
    <p>{props.data.site.siteMetadata.author}</p>
    </footer>
  </div>
)

export default Article

export const query = graphql`
  query Article($id: String) {
    site {
      siteMetadata {
        title
        description
        author
      }
    }
    contentfulArticle(id: { eq: $id }) {
      title
      subtitle
      slug
      para {
        para
      }
    }
  }
`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM