【问题标题】:Data from parent to children coming with delay in react从父母到孩子的数据延迟反应
【发布时间】:2021-10-03 09:14:41
【问题描述】:

我正在尝试将数据从父组件传递给子组件,但数据的延迟会破坏我的代码。这是子组件,第二个是父组件。我做错了什么? 数据应该在 App.js 中渲染之前传递给孩子。如何解决?

问题出在 Grid 组件中的“const rows = objectProps”中。数据来迟了

import React from "react";
import { DataGrid } from "@material-ui/data-grid";
import "./Grid.css";
    
const Grid = ({objectProps}) => {
  
  const rows = objectProps
 
  const columns = [
    { field: "id", hide: true },
    { field: "empId1", headerName: "Employee ID#1", width: 150 },
    { field: "empId2", headerName: "Employee ID#2", width: 150 },
    { field: "projectId", headerName: "Project ID", width: 150 },
    { field: "days", headerName: "Days worked", width: 150 },
  ];

  return (
    <div className="data-grid-container">
      <DataGrid rows={rows} columns={columns} />
    </div>
  );
};

export default Grid;

这是父组件

import React, { useState } from "react";
import './App.css';
import readLines from './helpers/calculateTeamDays'
import InputFile from "../src/components/input/InputFile";
import Grid from "../src/components/grid/Grid";


function App() {
  const [state, setState] = useState({});
  const [showGrid, setShowGrid] = useState(false);
  const [none, setNone] = useState(false);

  

  const addFile = (e) => {
    let fileList = e.target.files;
    let fileReader = new FileReader();

    fileReader.onload = () => {

      let linesArray = fileReader.result.split("\n")

      let objects = readLines(linesArray);
      objects.forEach((obj, i) => obj.id = i)

      if (objects) {
        setShowGrid(true)
        setState(objects)
        setNone(false)
        console.table(objects)

      } else {
        setNone(true)
        setShowGrid(false)
       }
    }

    fileReader.readAsText(fileList[0])
  }

  return (
    <div className="app">
      <h1>Find two team members worked longest on project together</h1>

      <InputFile add={addFile} />
      {none ? <h2>No one worked in a team!</h2> : null}
      {showGrid ? <Grid objectProps={state} /> : null}
    </div>
  );
}

export default App;

【问题讨论】:

  • 最简单的方法是在渲染Grid 组件之前确保您的state 对象不为空。 showGrid 可能在 state 对象被属性填充之前是 true,因为所有这些 useState 更新函数都是异步的,并且取决于 React 如何批处理它们,您永远不能期望它们按顺序运行他们被称为。

标签: reactjs frontend use-effect use-state


【解决方案1】:
import React, { useState } from "react";
import './App.css';
import readLines from './helpers/calculateTeamDays'
import InputFile from "../src/components/input/InputFile";
import Grid from "../src/components/grid/Grid";


function App() {
  const [state, setState] = useState({});

  const addFile = (e) => {
    let fileList = e.target.files;
    let fileReader = new FileReader();

    fileReader.onload = () => {

      let linesArray = fileReader.result.split("\n")

      let objects = readLines(linesArray);
      objects.forEach((obj, i) => obj.id = i)

      if (objects) {    
        setState(objects)
      } else {
setState(null) 
}
    }

    fileReader.readAsText(fileList[0])
  }

  return (
    <div className="app">
      <h1>Find two team members worked longest on project together</h1>
      <InputFile add={addFile} />
      {state && Object.entires(state).length > 0 ?
       <Grid objectProps={state} /> : 
        <h2>No one worked in a team!</h2>} 
    </div>
  );
}

export default App;

【讨论】:

  • 没有延迟仍然会使应用程序崩溃。我想我需要在渲染之前发送数据,但我不知道该怎么做
  • 更新了所有代码,如果它还在发生请告诉我
  • 不,现在我得到错误 object.entries is not a function 这是源代码github.com/xakepa/FirstName-LastName-employees 程序输入文件是带有名称的纯文本文件。例如 Frank, proectX, 2020-01-01, 2021-02-03 Frank, ASD, 2017-01-01, 2019-02-03 Ivan, ASD, 2018-01-01, 2018-06-03 Martin, ASD , 2020-3-2, 2020-3-6 Frank, ASD, 2020-3-20, 2020-3-25 Frank, OI9, 2017-3-7, 2020-3-25 Ivan, proectX, 2020-01- 01, 2021-02-03
  • @Simba 上面的代码有错别字,应该是Object.entries(state),但你也可能在不支持Object.entries的浏览器中运行它,在这种情况下我建议使用Object.keys 方法。
  • 我不得不删除 object.entries() 现在它可以工作 {state && state.length > 0 ? (
猜你喜欢
  • 2020-01-09
  • 2018-10-05
  • 2018-07-07
  • 2020-09-19
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
相关资源
最近更新 更多