简体   繁体   English

如何使用 gatsby 将值传递给 GraphQL 查询并做出反应?

[英]How do I pass a value into an GraphQL query using gatsby and react?

I am trying to grab something from my database using graphql by the items id, and I am not sure how to pass a value into graphql to grab it.我正在尝试使用 graphql 通过项目 ID 从我的数据库中获取一些东西,但我不确定如何将值传递给 graphql 来获取它。


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

const MyCustomComponent = (props) => {
  // How do I pass this value
  let theValueIWantToPass = 5

  // into this query
  const data = useStaticQuery(graphql`
    query MyQuery {
      mongodbItems(id: {eq: "5"}){
        id
      }
    }
  `)


  return (
    <div>

    </div>
  )
}


export default MyCustomComponent

Static queries are called "static" because the have a single, unchanging value that is calculated when you run gatsby build . Static 查询被称为“静态”查询,因为它有一个在您运行gatsby build时计算的单一不变的值。 So by their very nature they cannot not take variables.因此,就其本质而言,他们不能不接受变量。

Depending on what you're trying to achieve you might use a page query .根据您要实现的目标,您可能会使用页面查询 It can take parameters but these parameters are fixed for each page (in other words: a single query is executed for each page when you run gatsby build , so you must know your variables for each page at build time).它可以接受参数,但这些参数对于每个页面都是固定的(换句话说:当您运行gatsby build时,会为每个页面执行一个查询,因此您必须在构建时知道每个页面的变量)。

It is not possible to execute Gatsby Graph QL queries after the build step.在构建步骤之后无法执行 Gatsby Graph QL 查询。 If, for example, your something entered by the user, you can not use Gatsby queries for that use case.例如,如果用户输入了您的内容,则您不能对该用例使用 Gatsby 查询。

Extending @ehrencrona' answer .扩展@ehrencrona 的答案 It's completely right that the main purpose to use a staticQuery rather than a standard common query is to fetch static data and because of this, they don't accept values plus other limitations/differences such as:使用staticQuery而不是标准常见查询的主要目的是获取 static 数据是完全正确的,因此,它们不接受值以及其他限制/差异,例如:

  • Page queries can accept variables (via pageContext ) but can only be added to page components页面查询可以接受变量(通过pageContext )但只能添加到页面组件
  • StaticQuery does not accept variables (hence the name "static"), but can be used in any component, including pages StaticQuery不接受变量(因此称为“静态”),但可以在任何组件中使用,包括页面
  • StaticQuery does not work with raw React.createElement calls; StaticQuery不适用于原始 React.createElement 调用; please use JSX, eg <StaticQuery />请使用 JSX,例如<StaticQuery />

Reference: https://www.gatsbyjs.org/docs/static-query/参考: https://www.gatsbyjs.org/docs/static-query/

However, they have a huge benefit rather than a page query due to the appearance of React Hooks (since v16.8 ) and it's the use of useStaticQuery hook.然而,由于 React Hooks 的出现(自v16.8起),它们有一个巨大的好处而不是页面查询,并且它使用了useStaticQuery钩子。 Despite having (almost) the same limitations as the staticQuery they have these benefits.尽管具有(几乎)与staticQuery相同的限制,但它们具有这些好处。

  • Much more cleaner更清洁
  • Much more readable更具可读性
  • Much more atomized更加雾化

From:从:

<StaticQuery
  query={graphql`
    query {
      site {
        siteMetadata {
          title
        }
      }
    }
  `}
  render={data => (
    <header>
      <h1>{data.site.siteMetadata.title}</h1>
    </header>
  )}
/>

To:至:

const Header = () => {
  const { title } = useSiteMetadata()

  return (
    <header>
      <h1>{title}</h1>
    </header>
  )
}   

Reference: https://www.gatsbyjs.org/blog/2019-02-20-introducing-use-static-query/参考: https://www.gatsbyjs.org/blog/2019-02-20-introducing-use-static-query/

More information in Gatby's documentation about useStaticQuery hook . Gatby 的文档中有关useStaticQuery hook 的更多信息。

For the answer, you should use a "hardcoded" value there:对于答案,您应该在那里使用“硬编码”值:

  const data = useStaticQuery(graphql`
    query MyQuery {
      mongodbItems(id: {eq: "5"}){
        id
      }
    }
  `)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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