【问题标题】:How to re-render/update the table in react js after CRUD operations?CRUD操作后如何在react js中重新渲染/更新表?
【发布时间】:2018-10-28 12:53:08
【问题描述】:

我使用了简单的表格(代码附加在下面)并尝试查找许多参考资料,但找不到适合我的问题的解决方案。该表显示正常,但是当添加、更新或删除任何数据时问题就开始了。除非刷新页面,否则它不会被反映。我尝试过的几种方法是更新状态但无济于事。我什至尝试过强制更新组件但徒劳无功。我什至尝试渲染组件。

<div className="table">
  <table className="table table-striped table-padding">
    <thead>
      <tr className="heading">
        <th className="col-md-2">Image</th>
        <th className="col-md-3">Office Name</th>
        <th className="col-md-5">Address</th>
        <th className="col-md-1"></th>
        <th className="col-md-1"></th>
      </tr>
    </thead>
    <tbody>
      {
        (this.state.dataUpdated) &&
          this.state.data.map((list, index) => {
            return (
              <tr key={index}>
                <td><img style={{height: '30px', width: '30px'}} src={list.imageData} alt="icon"/></td>
                <td>{list.name}</td>
                <td>{list.address}</td>
                <td><button type="button" onClick={(e) => this.handleEdit(list.officeId)}><i className="fa fa-pencil" aria-hidden="true"></i></button></td>
                <td><button type="button" onClick={(e) => this.handleDelete(list.officeId)}><i className="fa fa-trash" aria-hidden="true"></i></button></td>
              </tr>
            )

          })
        }
      </tbody>
   </table>
</div>

handleEdit(id) {
  this.state.data.map(row => {
    if(row.officeId === id) {
      return (
        this.setState({ displayData: row })
      )
    }
  });
  this.displayOfficeList();
}

handleDelete(id) {
  const { dispatch } = this.props;
  dispatch(commonActions.deleteOffice(id));
  this.displayOfficeList();
}

displayOfficeList() {
  this.toggleFlag();
  commonServices.getOfficeList().then((res) => {
    let demo = res;
    this.setState({ data: demo.content});
    this.setState({ dataUpdated: true});
  });
}

【问题讨论】:

  • 请添加handleEdit、handleDelete功能代码
  • 向我们展示您如何处理数据的删除和添加。
  • @Dieshundefined 代码已更新。
  • @null 代码已更新。请检查。

标签: reactjs html-table crud page-refresh rerender


【解决方案1】:

工作得很好。在添加/更新或删除条目的响应之前调用用于显示列表的 API 时出错。底线是,如果状态得到更新,组件会重新渲染自己。只需要小心,不要像我一样搞乱 API 调用。

【讨论】:

    【解决方案2】:

    每次执行添加、编辑和删除等操作时,您都需要更新数据状态。

    使用componentWillReceiveProps,参考以下链接

    React doesn't reload component data on route param change

    你可以在这里找到官方文档https://reactjs.org/docs/state-and-lifecycle.html

    还有 https://hackernoon.com/reactjs-component-lifecycle-methods-a-deep-dive-38275d9d13c0

    【讨论】:

    • 我确实更改了状态并尝试渲染表格。仍会参考您提到的链接,并会就此与您联系。
    • 这个表是我组件的一部分。所以,我不明白 componentWillReceiveProps 会有什么帮助,因为我的道具没有改变。请解释一下。
    • 如果您尝试使用反应路由器访问状态,将使用 componentWillReceiveProps。即(例如,/person/:id),您可能希望查看不同的人员详细信息,例如 /person/1 或 /person/2。因此,此时您需要在 componentWillReceiveProps 中更改为您的表提供服务的数据状态。我发布的链接供您将来参考。我会尽快发布答案,这将解决您的问题。
    • 这就是我的观点。我没有使用反应路由器重新渲染表格。我只想执行 CRUD 操作并将结果显示在表格中。
    • 每当您执行一些操作,例如handleEdit 或handleDelete,您只需要更新状态(this.state.data)。这样,它将自动呈现,无需刷新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2016-12-11
    • 2019-11-03
    • 2020-11-06
    • 2016-12-30
    • 1970-01-01
    • 2013-06-02
    相关资源
    最近更新 更多