简体   繁体   English

React:将 graphql 道具传递给另一个组件

[英]React: Passing a graphql prop to another component

I have a dashboard with a table component inside it.我有一个仪表板,里面有一个表格组件。 I am making a graphql call in the dashboard and need to pass the data recieved into the table like this.我正在仪表板中进行 graphql 调用,需要像这样将收到的数据传递到表中。 However I can't get the data to show up inside the table component.但是我无法让数据显示在表格组件内。

Here is my approach.这是我的方法。 Please let me know what I'm missing请让我知道我错过了什么

Dashboard.js仪表板.js

import React from "react";
import "bootstrap/js/src/collapse.js";
import DashboardTable from "../DashboardTable";
import { API } from "@aws-amplify/api";
import config from "../../aws-exports";
import * as queries from "../../graphql/queries";

export default function Dashboard() {
  var opportunityTable;

  API.configure(config);
  async function asyncCall() {
    const opportunityTable = await API.graphql({
      query: queries.listMockOppsTables,
    });
    // console.log(opportunityTable.data.listMockOppsTables); // result: { "data": { "listTodos": { "items": [/* ..... */] } } }
  }

  asyncCall();

  return (
    <div>
      <div className="container py-5">
        <DashboardTable
          data={opportunityTable.data.listMockOppsTables}
        ></DashboardTable>
      </div>
    </div>
  );
}

Table (receiving prop data)表格(接收道具数据)

import React from "react";
import "bootstrap/js/src/collapse.js";
require("../opportunityData.json");

export class Opportunity extends React.Component {
   constructor(props) {
    super(props);
    this.state = {
      opportunityData: this.props.items,
    };
  }
  render() {
     console.log(this.opportunityData);// returns nothing

    return (
      <div>
        <section class="py-5 mt-5">
          <div class="container py-5">
            <div class="table-responsive">
              <table class="table">
                <thead>
                  <tr>
                    <th>Shadow ID</th>
                    <th>Role Title</th>
                    <th>Interview Type</th>
                    <th>Level</th>
                    <th>Date and Time</th>
                    <th># Requests</th>
                    <th>Make Request</th>
                  </tr>
                </thead>
                {this.opportunityData.map((opportunity) => (
                  <tbody>
                    <tr>
                      <td>{opportunity.shadow_id}</td>
                      <td>{opportunity.job_title}</td>
                      <td>{opportunity.shadow_type}</td>
                      <td>4</td>
                      <td>
                        {opportunity.shadow_datetime}
                        <br />
                        {opportunity.shadow_datetime}
                      </td>
                      <td>2</td>
                      <td>
                        <div class="btn-group" role="group">
                          <button
                            class="btn btn-primary"
                            type="button"
                            style={{
                              backgroundColor: "#ff9900",
                              border: 0,
                            }}
                          >
                            Shadow
                          </button>
                          <button
                            class="btn btn-primary"
                            type="button"
                            style={{
                              backgroundColor: "#ffac2f",
                              border: 0,
                            }}
                          >
                            Reverse Shadow
                          </button>
                        </div>
                      </td>
                    </tr>
                  </tbody>
                ))}
              </table>
            </div>
          </div>
        </section>
      </div>
    );
  }
}
export default Opportunity;

You're problem is either with your method of passing state or with you querying.你的问题要么是你传递 state 的方法,要么是你查询的问题。 One issue in your code is by practice in React, you want state to be managed as a stateful value with the useState() hook.您的代码中的一个问题是 React 中的实践,您希望 state 使用useState()挂钩作为有状态值进行管理。 You will have to verify your data is loaded before passing it into the hook.在将数据传递到挂钩之前,您必须验证数据是否已加载。 This would look like:这看起来像:

const [opportunityTable, changeOpportunityTable] = useState(asyncCall());

Also you should've posted your DashBoardTable component instead of your Oppurtunity component which you don't seem to import in DashBoard to begin with.此外,您应该发布您的 DashBoardTable 组件而不是您似乎没有在DashBoard中导入的Oppurtunity组件。 Here's a little skeleton这是一个小骨架

const DashBoardTable = ({data}) => {
   //whatever code you may need
   return (
    //other formatting
     data.map((key) => {
         //access with data.<VARIABLE>
     }
);
} 

export default DashBoardTable;

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

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