GraphQL 查询适用于 Gatsby 页面,但不适用于类组件

IT技术 reactjs graphql gatsby prismic.io
2021-05-17 22:11:34

有几个类似的问题,但没有一个能帮助我真正理解在(类)组件中使用 GraphQL,而不是 pages 文件夹中的那些。

我的项目结构如下所示:

-src
--components
---aboutBody
----index.js
--pages
---about.js

我有一个名为about(Prismic 单页类型)的页面组件,并设置了一些组件来“填充”这个页面(为了更好的可读性进行了清理)。

class AboutPage extends Component {

  render() {
    return (
        <LayoutDefault>
          <AboutBody
            introHeadline={this.props.data.prismicAbout.data.intro_headline.text}
            introParagraph={this.props.data.prismicAbout.data.intro_paragraph.text}
          />
        </LayoutDefault>
    )
  }

}

export default AboutPage

这就是我的查询的样子(在两个文件中都是这样的):

export const aboutQuery = graphql`
  query About {
    prismicAbout {
      data {

        # Intro Block
        intro_headline {
          text
        }
        intro_paragraph {
          text
        }
      }
    }
  }
`

(如果我在底部缺少一个括号,这是由于清理了 SO 的查询示例——如前所述,它在我的页面组件中工作)。

我的 graphql 查询位于AboutPage页面组件的底部它就像一个魅力和预期的那样。

但是为了稍微清理一下这个页面,我想创建适当的组件并将我的查询放在每个组件中(例如aboutBodyaboutCarousel),再次清理一下:

class AboutBody extends Component {

  render() {

    return (
      <StyledIntro>
        <h3>About</h3>
        <h1>{this.props.data.prismicAbout.data.intro_headline.text}</h1>
      </StyledIntro>
    )
  }

}

export default AboutBody

我从我的about页面组件中删除了查询并将其放入我的AboutBody组件中(正是如上所示的方式)。

但是这样它总是返回错误Cannot read property 'prismicAbout' of undefined(我什至不能控制台记录数据,它总是返回相同的错误)。

import { graphql } from "gatsby"在两个文件中都使用过。

长话短说,我怎样才能实现在我的类组件中放置一个查询并只渲染组件而不像这样在我的页面组件中澄清props:

class AboutPage extends Component {

  render() {
    return (
        <LayoutDefault>
          <AboutBody />
        </LayoutDefault>
    )
  }

}

一些博客文章提到了 GraphQL 查询片段,但不确定这是否是正确的用例,或者它是否只是一个愚蠢的初学者错误......

3个回答

那是因为你不能在你的组件中使用这样的 graphql。

要在组件中使用 graphql,您有两个选择:useStaticQuery函数或StaticQuery组件,均来自 graphql

对于useStaticQuery

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

const MyElement = () => {
  const data = useStaticQuery(graphql`
     query About {
       prismicAbout {
         data {
           intro_headline {
             text
           }
           intro_paragraph {
             text
           }
         }
       }
     }
   `)

   return (
      <StyledIntro>
        <h3>About</h3>
        <h1>{this.props.data.prismicAbout.data.intro_headline.text}</h1>
      </StyledIntro>
   )
}

export default MyElement

staticQuery

import React from 'react'
import { StaticQuery, graphql } from 'gatsby';

const MyElement = () => {
   return(
      <StaticQuery
            query About {
              prismicAbout {
                data {
                   intro_headline {
                       text
                       }
                       intro_paragraph {
                          text
                       }
                   }
                }
             }
         `}
         render={data => (
            <StyledIntro>
               <h3>About</h3>
               <h1>{this.props.data.prismicAbout.data.intro_headline.text}</h1>
            </StyledIntro>
         )}
      />
   )
}

export default MyElement

希望能帮到你!

您只能在页面组件中使用类似的查询。一种选择是只在页面中查询它,然后将数据作为props传递给您的组件。另一种是在组件中使用静态查询。

如果您的查询中有变量,那么您就不能使用静态查询。在这种情况下,您应该在页面中查询所有内容,然后将其传入,或者您可以将与该组件相关的查询部分放在该组件文件中的片段中,然后在页面查询中使用该片段。

在组件中使用片段然后将数据传递到组件中的示例:

// MyComponent.js
import React from "react"
import { graphql } from 'gatsby'

const MyComponent = (props) => {

  const { myProp: { someData } }  = props

  return (
    <div>
        my awesome component
    </div>
  )
}

export default MyComponent

export const query = graphql`
  fragment MyAwesomeFragment on Site {
     someData {
         item
     }
  }
`
// MyPage.js
import React from "react"
import { graphql } from "gatsby"

import MyComponent from "../components/MyComponent"

export default ({ data }) => {
  return (
    <div>
      {/*
        You can pass all the data from the fragment
        back to the component that defined it
      */}
      <MyComponent myProp={data.site.someData} />
    </div>
  )
}
export const query = graphql`
  query {
    site {
      ...MyAwesomeFragment
    }
  }
`

Gatsby 文档中阅读有关使用片段的更多信息

如果您需要在基于类的组件中呈现查询。这对我有用:

import React, { Component } from 'react';
import { StaticQuery, graphql } from 'gatsby';

class Layout extends Component {
  render() {
    return (
      <StaticQuery
        query={graphql`
          query SiteTitleQuery {
            site {
              siteMetadata {
                title
              }
            }
          }
        `}
        render={data => {
          return (
            <main>
              {!data && <p>Loading...</p>}
              {data && data.site.siteMetadata.title}
            </main>
          )
        }}
      />
    );
  }
}