【问题标题】:onChange event re-renders the Entire TableonChange 事件重新呈现整个表
【发布时间】:2019-07-18 12:08:29
【问题描述】:

我用react-tablenpm包,我把表需要的所有数据都存储在state里

componentDidMount() {
    this.props.client
      .query({
        query: ALL_SKUS
      })
      .then(({ data }) => {
        const skus = removeTypename(data.allSkuTypes);
        const newData = skus.map((sku, index) => ({
          serial: index + 1,
          ...sku
        }));
        this.setState({ data: newData });
      })
      .catch(err => {
        console.log(err);
      });
  }

这就是我的专栏的“名称”字段的样子

 {
        Header: 'SKU Name',
        headerClassName: 'vt-table-header',
        accessor: 'name',
        maxWidth: 350,
        Cell: this.renderEditable
      },

这是事件处理程序

 renderEditable = ({ index, column }) => {
    const { data } = this.state;

    return (
      <InputGroup
        onChange={e => {
          const newData = [...data];
          newData[index][column.id] = e.target.value;
          this.setState({ data: newData });
        }}
        value={data[index][column.id]}
      />
    );
  };

最后这就是所有数据进入反应表的方式

<ReactTable
          loading={data.length === 0 ? true : false}
          showPagination={false}
          className="mt-3 text-center"
          data={data}
          columns={columns}
        />

我尝试从 Input 中删除 value 属性,然后向其添加 onBlur ,同时解决了最初从查询中获取数据时启用的性能问题。

在我的应用程序中,我也以许多复杂的形式面临这个问题,任何帮助将不胜感激

【问题讨论】:

    标签: reactjs forms state onchange


    【解决方案1】:

    这是一个想法。

    而不是这样做,它为我认为的表格中的单元格提供了一个组件InputGroup:

     renderEditable = ({ index, column }) => {
        const { data } = this.state;
    
        return (
          <InputGroup
            onChange={e => {
              const newData = [...data];
              newData[index][column.id] = e.target.value;
              this.setState({ data: newData });
            }}
            value={data[index][column.id]}
          />
        );
      };
    

    如何创建一个单独的组件来控制它自己的状态,而不是为每个单元格更改更改整个表状态,为什么不使用一个组件 CELL 来更改它自己的状态与任何其他可能的 POST 请求......等等。

    这会将渲染范围缩小到基于Cell,而不是渲染整个Table。

    几乎是一样的:

    class Cell extends React.Component {
      state = { ...yourCellData }
    
      componentDidMount() {
        this.setState({ ...this.props.youCellData }); //any properties passed from the main component.
      }
    
      render(){
        const { index, column } = this.props;
    
        return (
          <InputGroup
            onChange={e => {
              const newData = [...this.state.data];
              newData[index][column.id] = e.target.value;
              this.setState({ data: newData }); //this now modifies the Cell data instead of the whole table
            }}
            value={data[index][column.id]}
          />
        );
      }
    
    }
    

    可能适用于:

     {
            Header: 'SKU Name',
            headerClassName: 'vt-table-header',
            accessor: 'name',
            maxWidth: 350,
            Cell: (index, column) => <YourNewCellComponent index={index} column={column} />
     },
    

    我希望这能以某种方式为您提供一个可能解决问题的总体方法

    【讨论】:

    • 但是如果我需要该行中多个单元格的信息怎么办。例如,当用户点击桌子上的检查按钮时,我需要点击具有 SKU 名称、容量和皮重的突变
    猜你喜欢
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 2018-05-27
    • 1970-01-01
    相关资源
    最近更新 更多