简体   繁体   English

我无法在 gatsby js 上设置状态 graphql 查询

[英]i can't setState graphql queries on gatsby js

I have tried to fetch data onClick with GraphQL but for some reason I can't setState multiple queries.我试图获取数据onClick与GraphQL但由于某种原因,我不能setState多个查询。 It says (Loading Static query)它说(加载静态查询)

class Math extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      value: url,
      value2: texts[1],
      value3: queryOne,
    }
  }
  render() {
    return (
        <Sidebar>
          <div
            className="post"
            onClick={() =>
              this.setState({
                value: pablo,
                value2: texts[1],
               value3: queryTwo,
              })
            }
          >
            <img src={pablo} alt="pablo"/>
            <h3>1deg school</h3>
          </div>
          <div
            className="post"
            onClick={() =>
              this.setState({ value: clip, value2: texts[2], value3: queryOne })
            }
          >
            <img src={clip} alt="clip"/>
            <h3>2deg school</h3>
          </div>
        </Sidebar>
        <Content>
          <img src={this.state.value} alt="img"></img>
          <p>{this.state.value2}</p>
          <div className="posts">
            <StaticQuery
              query={queryOne}
              render={data => {
                return (
                  <div>
                    {data.allMarkdownRemark.edges.map(({ node }) => (
                      <Post
                        title={node.frontmatter.title}
                        path={node.frontmatter.path}
                        body={node.excerpt}
                        date={node.frontmatter.date}
                      />
                    ))}
                  </div>
                )
              }}
            />
          </div>
        </Content>
    )
  }
}
const queryOne = graphql`
  query MyQuery {
    allMarkdownRemark {
      edges {
        node {
          frontmatter {
            title
            date
            path
          }
          excerpt
        }
      }
    }
  }
`
const queryTwo = graphql`
  query MyQuery {
    allMarkdownRemark(filter: { fileAbsolutePath: { regex: "/001/" } }) {
      edges {
        node {
          frontmatter {
            titledatepath
          }
          excerpt
        }
      }
    }
  }
`
export default Math

I can switch only between images, and it shows this warning:我只能在图像之间切换,并显示此警告:

We were unable to find the declaration of variable "value3", which you passed as the "query" prop into the我们无法找到变量“value3”的声明,您将其作为“查询”道具传递到declaration宣言

It's unclear to me what exactly you are trying to do but you can't pass variables for static queries.我不清楚你到底想做什么,但你不能为静态查询传递变量。 They must be truly static and the queries passed directly into query attribute:它们必须是真正静态的,并且查询直接传递到查询属性中:

<StaticQuery query={graphql`query MyStaticQuery {...}`} />

This way they can be discovered, extracted and transformed into static json during develop/build.通过这种方式,它们可以在开发/构建期间被发现、提取并转换为静态 json。

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

相关问题 如何在 gatsby-node.js 中执行 graphql 中的所有查询? - How to execute all queries in graphql inside gatsby-node.js? 在创建我的 gatsby-node.js 文件并正确指示通过 graphql 动态生成的页面后,我无法查看任何详细产品 - After creating my gatsby-node.js file with the pages properly instructed to be dynamically generated via graphql, I can't view any of detailed product Gatsby JS,无法在回调中创建节点 - Gatsby JS, Can't Create Node In Callback 我是否可以通过缩小 graphql 查询来遇到问题 - Are there problems that I can run into by minifying graphql queries 如何在 GraphQL 中进行异步查询? - How I can make asynchronous queries in GraphQL? Gatsby.js的GraphQL查询回调 - GraphQL query callbacks for Gatsby.js 在 Gatsby.js 中限制过滤的 Graphql 项 - Limit filtered Graphql items in Gatsby.js 无法让 {data.allFile.edges.map(({ node }) 使用 Gatsby + GraphQL 渲染多个图像 - Can't get {data.allFile.edges.map(({ node }) to render multiple images with Gatsby + GraphQL 如何在 graphql 中定义没有任何参数的突变或查询? - How can I define mutations or queries without any parameters in graphql? 我怎样才能让这个 anime.js 在 Gatsby 中工作 - How can I get this anime.js to work in Gatsby
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM