【问题标题】:React-Table not rendering dataReact-Table 不呈现数据
【发布时间】:2019-05-16 23:04:38
【问题描述】:

我正在尝试在 react-table 组件中呈现一些数据,但是数据没有加载。我已经用完全相同格式的虚拟数据进行了测试,它工作正常。但是,当我进行 API 调用并获取相同格式的数据并将其推送到我传递给反应表的数据列表时,该表不会呈现它。请帮我确定问题。干杯

设置列:

    columns: [
      {
        Header: "Employee ID",
        accessor: "EmployeeID"
      },
      {
        Header: "First Name",
        accessor: "FirstName"
      },
      {
        Header: "Last Name",
        accessor: "LastName"
      },
      {
        Header: "Date of Birth",
        accessor: "DateOfBirth",
      },
      {
        Header: "Status",
        accessor: "Status",
      },
      {
        Header: "Gender",
        accessor: "Gender",
      },
      {
        Header: "UpdatedDateUTC",
        accessor: "UpdatedDateUTC",
      }
    ]

数据是什么样子的:

{"EmployeeID":"63c571b3-bff0-4ce1-94f7-255c235580fa","FirstName":"Clive","LastName":"Thomas","Status":"ACTIVE","DateOfBirth":"/Date(697248000000+0000)/","Gender":"M","UpdatedDateUTC":"/Date(1533706298000+0000)/"}

我的 API 调用以及我如何将数据项保存到状态。 (我控制台记录了我得到的数据的值,并且格式正确)

fetch('http://localhost:3100/employees')
      .then((resp) => {
        return resp.json()
      })
      .then((data) => {
        let temp = this.state.posts;
        temp.push(data.Employees[1])
        this.setState({posts: temp})
        console.log(this.state.posts)
      })
      .catch((error) => {
        console.log(error, "catch the hoop")
      })

状态和“帖子”列表存储处于底部状态的帖子(带有虚拟数据):

state = {
    title: "Choose an Endpoint",
    activeOrg: "Orginisation",
    isExpanded: false,
    activeLink: 0,
    authLink:'',
    response: '',
    post: '',
    responseToPost: '',
    show: false,
    modalContent:"",
    token:'',
    verifier:'',
    org:'',
    orginisations: [
      { id: 1, name: "ANU"},
      { id: 2, name: "Bar"},
      { id: 3, name: "FANG"},
      { id: 4, name: "Atlassian"}
    ],
    list: [
      { id: 1, name: "Employees" },
      { id: 2, name: "Leave Applications" },
      { id: 3, name: "Pay Items" },
      { id: 4, name: "Payroll Calendars" },
      { id: 5, name: "Pay Runs" },
      { id: 6, name: "Pay Slips" },
      { id: 7, name: "Settings" },
      { id: 8, name: "Superfund Products" },
      { id: 9, name: "Timesheets" }
    ],
    columns: [
      {
        Header: "Employee ID",
        accessor: "EmployeeID"
      },
      {
        Header: "First Name",
        accessor: "FirstName"
      },
      {
        Header: "Last Name",
        accessor: "LastName"
      },
      {
        Header: "Date of Birth",
        accessor: "DateOfBirth",
      },
      {
        Header: "Status",
        accessor: "Status",
      },
      {
        Header: "Gender",
        accessor: "Gender",
      },
      {
        Header: "UpdatedDateUTC",
        accessor: "UpdatedDateUTC",
      }
    ],
    posts: [
      {"EmployeeID":"63c571b3-bff0-4ce1-94f7-255c235580fa","FirstName":"Clive","LastName":"Thomas","Status":"ACTIVE","DateOfBirth":"/Date(697248000000+0000)/","Gender":"M","UpdatedDateUTC":"/Date(1533706298000+0000)/"}
    ]
  }

渲染函数:

render() {
    let myClass=""
    let EndpointList = (
      <div>
        {this.state.list.map((i) => {
          i.id === this.state.activeLink ? myClass="endpoint activeLink" : myClass="endpoint"
          return <Endpoint 
            key={i.id}
            name={i.name}
            myClass={myClass}
            clicked={(event) => this.handleClickEndpoint(i, i.id)}/>
        })}
        </div>
    );
    let orgContainer = ""
    this.state.isExpanded ? orgContainer="orgItem expanded" : orgContainer="orgItem notExpanded"
    let OrgList = (
      <div className={orgContainer}>
        {this.state.orginisations.map((o) => {
          return <Orginisation
            key={o.id}
            name={o.name}
            clicked={(event) => this.handleClickOrg(o,o.id)}
          ></Orginisation>
        })}
      </div>
    );
    var activeContent=<ReactTable columns={this.state.columns} data={this.state.posts} noDataText={"Loading..."}></ReactTable>
    // const columns = Object.keys(this.state.data[0]).map((key, id)=>{
    //   console.log(key)
    //   return {
    //     Header: key,
    //     accessor: key,
    //   }
    // })
    return (
      <Router>
        <Route path='/' exact render={
          () => {
            return (
              <div className='authenticateContainer'>

                <a href={this.state.authLink} className='fill-div'>Click to Auntheticate</a> 
              </div>
            )
          }
        }/>
        <Route path='/home' render={
          () => {
            return (
              <div>
                <div className='sideBar'>
                  <div className='logoHolder'>
                    <img className='logo' alt='Logo' src={'./Assets/logo.png'}></img>
                  </div>
                  {EndpointList}
                  {OrgList}
                  <div style={{}} className="org button" onClick={this.expandOrg}>
                    <img className="orgLogo" alt='Logo' src={'./Assets/orgLogo.png'}></img>
                    {this.state.activeOrg}
                  </div>
                </div>
                <div className="container" id={this.state.title}>
                  {/* <button onClick={() => { this.setCredentials() }}>CLICK ME</button> */}
                  <div className="contentContainer">
                    <div className="head">
                      {this.state.title}
                    </div>
                    {activeContent}
                  </div>
                </div>
              </div>
            )
          }
        } />

      </Router> 
    );
  }
}

实例化反应表(也在上面的渲染函数中):

var activeContent=<ReactTable columns={this.state.columns} data={this.state.posts} noDataText={"Loading..."}></ReactTable>

我还打印了成功插入列表的虚拟数据以及未插入的 API 数据。如您所见,它们显然是相同的:

【问题讨论】:

  • 你所在的州是什么样的?
  • 您是否在渲染中渲染 activeContent?你能分享整个组件吗?
  • 我已经更新了@cullanrocks 的问题
  • 我只是在 App.js @Avanthika 的渲染方法中渲染活动内容
  • 我还是想看看你的完整初始状态。我不知道这是否有问题。

标签: javascript reactjs react-table


【解决方案1】:

不确定这是否会解决您的问题,但 IMO 您应该将其重构为

fetch('http://localhost:3100/employees')
      .then((resp) => {
        return resp.json()
      })
      .then((data) => {
        let temp = [...this.state.posts]
        temp.push(data.Employees[1])
        this.setState({
            posts: temp,
            data: data
        })
        console.log(this.state.posts) //this will not have the newest values yet, setState is async
      })
      .catch((error) => {
        console.log(error, "catch the hoop")
      })

对反应状态执行操作不是一个好习惯

【讨论】:

  • 设置“数据”状态的那一行实际上是从样板代码中复制过来的。现已删除。但感谢您发布一个没有回答问题的“答案”。
  • 如果使用示例数据工作正常,并且您的实际数据格式完全相同,那么您应该检查的第一件事(使用调试器)是按照您的需要设置状态。另一个潜在的问题是您的渲染方式(最重要的是当设置了 activeContent 变量时),但您没有包含渲染功能,只有 1 行。不幸的是,您没有提供足够的信息来“回答”您的问题
  • 我没有包括,所以它会更容易阅读,但添加了
【解决方案2】:

您为什么要推动员工[1]?那将是第二条记录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-10
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多