父子组件中的异步渲染:React

问题描述 投票:0回答:1

我有这种情况,其中我根据传递的值显示一组动态表。如果传递了一个空数组,则应显示No data found。在我向表发送值的情况下,所有表将首先显示“未找到数据”,然后显示实际的表内容。我不确定是什么原因造成的。

数据是异步加载的,它显示没有找到数据,然后显示的是实际内容。

我该如何解决?

我正在为此使用react-table v6。此方法还显示更多/更少显示实现,该实现基本上显示Show More上的所有项目以及Show Less时的默认值数量。

沙盒:https://codesandbox.io/s/react-table-row-table-ryiny?file=/src/index.js:0-1322

这是我的代码

父母

import * as React from "react";
import { render } from "react-dom";
import DataGrid from "./DataGrid";

const d1 = [{ name: "test", age: "20" }, { name: "test1", age: "15" }];
const d2 = [{ area: "area", pin: "123" }, { area: "area1", pin: "1245" }];
const c1 = [
  { Header: "Name", accessor: "name" },
  { Header: "Age", accessor: "age" }
];
const c2 = [
  { Header: "Area", accessor: "area" },
  { Header: "Pin", accessor: "pin" }
];
const d3 = [];
const c3 = [];

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data1: [],
      column1: [],
      data2: [],
      column2: [],
      data3: [],
      column3: []
    };
  }

  componentDidMount() {
    setTimeout(() => {
      this.setState({
        data1: d1,
        column1: c1
      });
    }, 2000);
    setTimeout(() => {
      this.setState({
        data2: d2,
        column2: c2
      });
    }, 2500);
    this.setState({
      data3: d3,
      column3: c3
    });
  }

  render() {
    return (
      <>
        <DataGrid data={this.state.data1} columns={this.state.column1} />
        <DataGrid data={this.state.data2} columns={this.state.column2} />
        <DataGrid data={this.state.data3} columns={this.state.column3} />
      </>
    );
  }
}

render(<App />, document.getElementById("root"));

儿童

import * as React from "react";
import ReactTable from "react-table";
import "react-table/react-table.css";

export default class DataGrid extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showMore: false
    };
  }

  toggleState = () => {
    this.setState(prevState => ({
      showMore: !prevState.showMore
    }));
  };

  formatData = () => {
    let arr = [];
    if (this.props.data && this.props.data.length > 0)
      arr = this.state.showMore ? this.props.data : this.props.data.slice(0, 2);
    return arr;
  };

  render() {
    const { showMore } = this.state;
    const { data, columns } = this.props;
    const showLink = data.length > 2;
    const subset = this.formatData();
    return (
      <>
        {showLink && (
          <button onClick={this.toggleState}>
            Show {showMore ? "Less" : "More"}
          </button>
        )}
        {data && data.length > 0 ? (
          <ReactTable
            showPagination={false}
            data={subset}
            columns={columns}
            minRows={0}
            NoDataComponent={() => null}
            loading={false}
          />
        ) : (
          "No data found"
        )}
      </>
    );
  }
}

非常感谢您的帮助!谢谢

javascript reactjs ecmascript-6 setstate react-table
1个回答
0
投票

因为您从index.js渲染了3个“ DataGrid”。在第3个DataGrid中,您传递一个空数据数组,以便显示“未找到数据”。它按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.