【问题标题】:React: How to wait on prop data before rendering child componentReact:如何在渲染子组件之前等待道具数据
【发布时间】:2022-12-03 17:58:20
【问题描述】:

我有一个名为 Dashboard 的父组件和一个名为 DashboardTable 的子组件。我能够在父元素中成功进行异步 graphql 调用并将数据传递给子组件。

问题:props 数据需要一秒钟来加载,所以它在获取数据之前返回 undefined 一秒钟(使用 console.log() 验证)。该延迟会导致映射函数出错。错误:无法读取未定义的属性(读取“地图”)

问题:如何让渲染器在渲染前等待道具数据加载。我试过像这样的条件 {this.props.data == undefined ? (wait) : (render)} 但那没有用(同样的错误)。

这是我的代码。请让我知道我做错了什么

仪表板表(子)

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

export class DashboardTable extends React.Component {
  constructor(props) {
    super(props);
  }

 

  render() {
    console.log(this.props.data.data); // !!this returns undefined one time then a second later it returns the props data I want :)

    return (
      <div>
        <div
          className="row row-cols-1 row-cols-md-2 mx-auto"
          style={{ maxWidth: 900 }}
        >
          {this.props.data.data.map((opportunity) => (
            <div className="col mb-4">
              <div>
                <a href="#">
                  <img
                    className="rounded img-fluid shadow w-100 fit-cover"
                    src="assets/img/products/awsLogo.jpg"
                    style={{
                      height: 250,
                    }}
                  />
                </a>
                <div className="py-4">
                  <span
                    className="badge mb-2"
                    style={{ margin: 2, backgroundColor: "#ff9900" }}
                  >
                    {opportunity.interview_type}
                  </span>
                  <span
                    className="badge bg mb-2"
                    style={{ margin: 2, backgroundColor: "#ff9900" }}
                  >
                    4
                  </span>
                  <span
                    className="badge bg mb-2"
                    style={{ margin: 2, backgroundColor: "#ff9900" }}
                  >
                    Reverse
                  </span>
                </div>
              </div>
            </div>
          ))}
        </div>
      </div>
    );
  }
}
export default DashboardTable;


仪表板(父级)这有效。它将 graphql 数据传递给孩子

import React, { useEffect, useState } 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() {
  API.configure(config);

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

  // initialize with empty array
  const [opportunityTable, changeOpportunityTable] = useState([]);
  //console.log(opportunityTable); // this works! returns a promise

  // call api to fetch data on mount
  useEffect(() => {
    const fetchData = async () => {
      const response = await asyncCall();

      changeOpportunityTable(response);
    };

    fetchData();
  }, []);

  return (
    <div>
      <section className="py-5 mt-5">
        <div className="container py-5">
          <h2 className="fw-bold text-center">
            Your upcoming shadowing events
            <br />
            <br />
          </h2>

          <DashboardTable data={opportunityTable}></DashboardTable>
        </div>
      </section>
    </div>
  );
}

【问题讨论】:

    标签: javascript reactjs react-native graphql mapping


    【解决方案1】:

    你需要这样做:

    { oppotrunityTable && opportynityTable.length ?
      <DashboardTable data={opportunityTable}></DashboardTable>
      : null
    }
    

    但是你有一个错误,不是因为这个而是因为你的数据是未定义的!检查你的数据!

    【讨论】:

    • 感谢您的回复。这消除了错误,但现在子组件中没有任何内容运行包括 console.log("helloworld")
    • 是的!!因为你的数据有误?通过父组件中的 console.log() 检查机会表))
    • 你是对的。对不起,我一开始不明白。我通过数据修复,现在在父级的控制台中看到它,但孩子仍然没有显示。有任何想法吗?
    • 如果您的数组数据在 opportunityTable.data 中,您需要更改状态并将条件更改为:opportynityTable.data & opportunities Table.data.length
    • 因为我认为你的机会表不是数组))
    猜你喜欢
    • 2023-04-01
    • 2016-09-15
    • 1970-01-01
    • 2019-07-21
    • 2020-02-11
    • 2021-05-02
    • 1970-01-01
    • 2021-04-27
    相关资源
    最近更新 更多