[英]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.