简体   繁体   English

在React组件中时如何从GraphQL传递数据

[英]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. 它返回给我的只是来自父母的idalt道具。

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}/>
  )
}

单击时如何将数据从一个反应组件传递到另一个反应组件<link tag< div><div id="text_translate"><p> 单击时如何将数据从一个反应组件传递到另一个反应组件</p><pre>[ { "id": 1, "title": "XYZ Title", "body": "www.xyz.com sample hits", "comments": [ { "id": 1, "postId": 1, "name": null, "email": "abc@cc.com", "body": "very simple body" } ] } ]</pre><p> 我有两个 JSX 页面,一个用于帖子,第二个 JSX 页面用于评论。 单击帖子时需要导航此帖子的评论页面。请参见下面的代码片段。 帖子.jsx</p><pre> &lt;tbody&gt; {posts.map((post) =&gt; ( &lt;tr key={post.id}&gt; &lt;td&gt; &lt;Link to={`/posts/${post.id}`} &gt; {post.title} &lt;/Link&gt; &lt;/td&gt; &lt;td&gt;{post.body}&lt;/td&gt;</pre><p> 我们如何获取和迭代相关评论数据以给出帖子 ID(单击帖子 ID 的链接时)。 单击帖子的post.id到comments.jsx时如何将评论数据从posts.jsx传递到comments.jsx</p><p> 评论.jsx</p><pre> &lt;tbody&gt; {comments.map((comment) =&gt; ( &lt;tr key={comment.id}&gt; &lt;td&gt;{comment.name}&lt;/td&gt; &lt;td&gt;{comment.email}&lt;/td&gt; &lt;td&gt;{comment.body}&lt;/td&gt;</pre></div> - How to pass data from one react component to other react component when clicking on <Link tag

暂无
暂无

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

相关问题 如何将数据从React组件传递到Apollo graphql查询? - How to pass data from react component to Apollo graphql query? 如何将从 graphql API 获取的数据传递给 React class 组件 - How to pass data fetched from graphql API to React class component 单击时如何将数据从一个反应组件传递到另一个反应组件<link tag< div><div id="text_translate"><p> 单击时如何将数据从一个反应组件传递到另一个反应组件</p><pre>[ { "id": 1, "title": "XYZ Title", "body": "www.xyz.com sample hits", "comments": [ { "id": 1, "postId": 1, "name": null, "email": "abc@cc.com", "body": "very simple body" } ] } ]</pre><p> 我有两个 JSX 页面,一个用于帖子,第二个 JSX 页面用于评论。 单击帖子时需要导航此帖子的评论页面。请参见下面的代码片段。 帖子.jsx</p><pre> &lt;tbody&gt; {posts.map((post) =&gt; ( &lt;tr key={post.id}&gt; &lt;td&gt; &lt;Link to={`/posts/${post.id}`} &gt; {post.title} &lt;/Link&gt; &lt;/td&gt; &lt;td&gt;{post.body}&lt;/td&gt;</pre><p> 我们如何获取和迭代相关评论数据以给出帖子 ID(单击帖子 ID 的链接时)。 单击帖子的post.id到comments.jsx时如何将评论数据从posts.jsx传递到comments.jsx</p><p> 评论.jsx</p><pre> &lt;tbody&gt; {comments.map((comment) =&gt; ( &lt;tr key={comment.id}&gt; &lt;td&gt;{comment.name}&lt;/td&gt; &lt;td&gt;{comment.email}&lt;/td&gt; &lt;td&gt;{comment.body}&lt;/td&gt;</pre></div> - How to pass data from one react component to other react component when clicking on <Link tag 如何在 React 中将数据从导航组件传递到主页组件? - How to pass data from Navigation Component to Home Component in React? 如何将数据从同级组件传递到组件 - How to pass data to a component from sibling component react 如何在 React 功能组件中将数据从孙子传递到父组件? - How to pass data from Grandchild to Parent component in React Functional Component? 如何在 React 中将数据从一个组件传递到另一个组件/父级 - How to pass data from one component to another component/parent in React React 如何将数据从组件传递到另一个组件 - how to pass data from component to another component in React 如何将graphQL查询变量传递到修饰的反应组件中 - How to pass graphQL query variable into a decorated react component react,gatsby-如何在React类组件中访问GraphQL查询数据 - react, gatsby - how to access GraphQL query data in react class component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM