[英]How to pass data from GraphQL when in a React Component
I have a Billboard component which is imported on my home page like so : 我有一个Billboard组件,它像这样导入到我的主页上:
<Billboard id="successful_network_ebook_billboard"/>
The content of the component is : 该组件的内容是:
import React, {Component} from "react";
import T from 'i18n-react'
import Link from "../components/LocalizedLink";
import Tank from "../components/Tank";
import CTA from "../components/CTA";
import Img from "gatsby-image"
import {graphql} from "gatsby"
import "../styles/css/components/Billboard.css";
class Billboard extends Component {
successful_network_ebook_billboard = () => {
return (
<section id={this.props.id} className="Billboard">
<Img className="bg" fluid={this.props.data.bgNetwork.childImageSharp.fluid} alt={this.props.alt}/>
<Tank className="content">
<div className="text">
<h3>Title</h3>
<p>Paragraph</p>
<CTA to="/page">Click here</CTA>
</div>
</Tank>
</section>
)
}
render() {
switch (this.props.id) {
/* There are more cases */
case "successful_network_ebook_billboard":
return (this.successful_network_ebook_billboard());
default:
return (
<div></div>
)
}
}
}
export default Billboard
require("../queries/fragment.headers");
export const queryBillboardImages = graphql `
query BillboardImages {
bgNetwork: file(relativePath: { eq: "assets/images/healthcare/banner_soc2_bg.jpg" }) {
...fluidHeader
}
}
`
When I run the code, everything loads fine, except the image I'm trying to load via GraphQL. 当我运行代码时,除了我试图通过GraphQL加载的图像之外,其他所有东西都可以正常加载。 I get the message :
我收到消息:
(Billboard, ) TypeError: Cannot read property 'bgNetwork' of undefined
Clearly "data" is empty, but I think I'm using the syntax that's proposed everywhere. 显然“数据”是空的,但是我认为我使用的是到处都有的语法。
I tried getting this.data instead of this.props.data ; 我尝试获取this.data而不是this.props.data ; I get the same error.
我犯了同样的错误。
I tried converting the component to a const which seemed to offer a more "direct" syntax, but I got a different, more esoteric error. 我尝试将组件转换为似乎提供更“直接”语法的const ,但是却遇到了另一个更深奥的错误。
I want to know how to pass the result of the GrphQL call into my component. 我想知道如何将GrphQL调用的结果传递到我的组件中。 When I do it on a page, it's as simple as destructuring this.props.data into a constant and then it's ready to use.
当我在页面上执行此操作时,就像将this.props.data分解为一个常量然后就可以使用一样简单。 But when inside a component it doesn't seem to work that way.
但是当在组件内部时,它似乎无法正常工作。
If I do a 如果我做一个
console.log(data)
all it returns me is the id and alt props that come from the parent. 它返回给我的只是来自父母的id和alt道具。
Thank you for your help ! 谢谢您的帮助 !
graphql query
can only be used for Pages and not all components. graphql query
只能用于Pages,而不能用于所有组件。 For components you need to use StaticQuery
component or with useStaticQuery
with the latest version of react 对于组件,您需要使用
StaticQuery
组件或将useStaticQuery
与最新版本的react一起使用
export default (props) => (
<StaticQuery
query={graphql `
query BillboardImages {
bgNetwork: file(relativePath: { eq: "assets/images/healthcare/banner_soc2_bg.jpg" }) {
...fluidHeader
}
}
`}
render={data => (
<Billboard {...props} data={data}/>
)}
/>
);
or using useStaticQuery
或使用
useStaticQuery
export default (props) => {
const data = useStaticQuery(graphql `
query BillboardImages {
bgNetwork: file(relativePath: { eq: "assets/images/healthcare/banner_soc2_bg.jpg" }) {
...fluidHeader
}
}
`)
return (
<Billboard {...props} data={data}/>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.