简体   繁体   English

未捕获的 TypeError:无效的解构不可迭代实例的尝试

[英]Uncaught TypeError: Invalid attempt to destructure non-iterable instance

I'm using react-table to make a DataTable component.我正在使用 react-table 制作一个 DataTable 组件。 Here is my component's code:这是我的组件的代码:

const DataTable: React.FC<IDataTable<any>> = (props: IDataTable<any>) => {
  const { columns, data, className, rowSelection, onChange, ...rest } = props;
  let tableColumns: HeaderColumn<any>[] = columns;
  useMemo(() => tableColumns, [rowSelection, columns]);
  const {
    getTableProps,
    headerGroups,
    rows,
    prepareRow,
    state: [{ selectedRows }],
  } = useTable({ columns: tableColumns, data }, useRowSelect);
  useEffect(() => onChange && onChange(selectedRows), [selectedRows]);
  return (
    <table {...getTableProps()} className={`ods-data-table ${className}`} {...rest}>
      <thead>
        {headerGroups.map((headerGroup, index) => (
          <tr key={index}>
            {headerGroup.headers.map((column, index) => (
              <th key={index} {...column.getHeaderProps()}>
                {column.render('Header')}
              </th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody>
        {rows.map(
          (row, i) =>
            prepareRow(row) || (
              <tr {...row.getRowProps()} tabIndex={i} key={i}>
                {row.cells.map((cell, i) => {
                  return (
                    <td {...cell.getCellProps()} key={i}>
                      {cell.render('Cell')}
                    </td>
                  );
                })}
              </tr>
            )
        )}
      </tbody>
    </table>
  );
};

When I try to use this component and pass the data, I get this error:当我尝试使用此组件并传递数据时,我收到此错误:
"Uncaught TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a Symbol.iterator method." “未捕获的 TypeError:解构不可迭代实例的无效尝试。为了可迭代,非数组对象必须具有 Symbol.iterator 方法。”
Here is how I'm trying to pass the data to the DataTable component.这是我尝试将数据传递给 DataTable 组件的方式。

const tableColumns: HeaderColumn<any>[] = [
  {
    Header: 'sample text',
    accessor: (object) => object.title,
  },
  {
    Header: 'sample text',
    accessor: (object) => object.price,
  },
];
const tableData = [
    {
      title: 'title1',
      price: 'price1',
    },
    {
      title: 'title2',
      price: 'price2',
    },
    {
      title: 'title3',
      price: 'price3',
    },
  ];
<DataTable columns={tableColumns} data={tableData} />

The error in the console says it is in DataTable component in this line (I'm not sure about it):控制台中的错误表明它在这一行的 DataTable 组件中(我不确定):

let tableColumns: HeaderColumn<any>[] = columns;

I'm using the exact component in another project and passing same data, but there isn't such error.我在另一个项目中使用确切的组件并传递相同的数据,但没有这样的错误。 I searched and found same questions in stackoverflow but couldn't solve the problem.我在stackoverflow中搜索并发现了相同的问题,但无法解决问题。 Could anyone help me in this regard?有人可以在这方面帮助我吗?
If my question is not complete or needs more info please let me know.如果我的问题不完整或需要更多信息,请告诉我。

I found the line of code which throws the error and I could solve the problem.我找到了引发错误的代码行,我可以解决问题。
Here in this line when I am using react-table hook (useTable):当我使用 react-table 钩子(useTable)时,在这一行中:

 const {
    getTableProps,
    headerGroups,
    rows,
    prepareRow,
    state: [{ selectedRows }],
  } = useTable({ columns: tableColumns, data }, useRowSelect);

I changed the array to be an object and the problem is gone.我将数组更改为 object,问题就消失了。 Here is the final code:这是最终代码:

 const {
    getTableProps,
    headerGroups,
    rows,
    prepareRow,
    state: { selectedRows },
  } = useTable({ columns: tableColumns, data }, useRowSelect);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的拒绝(TypeError):破坏非迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to destructure non-iterable instance TypeError:对不可迭代实例 React/Jest 的解构尝试无效 - TypeError: Invalid attempt to destructure non-iterable instance React/Jest 解构不可迭代实例的无效尝试 - Invalid attempt to destructure non-iterable instance React Uncaught TypeError:传播不可迭代实例的无效尝试 - React Uncaught TypeError: Invalid attempt to spread non-iterable instance 解构不可迭代实例的无效尝试 - React Native - Invalid attempt to destructure non-iterable instance - React Native Jest 快照测试 TypeError:尝试解构不可迭代实例无效 - Jest snapshot testing TypeError: Invalid attempt to destructure non-iterable instance React,获取 TypeError:在将全局状态与 Context 结合使用时,尝试解构不可迭代的实例无效 - React, Getting TypeError: Invalid attempt to destructure non-iterable instance when using global state with Context TypeError:无效的解构不可迭代实例的尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() - TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() Redux Web扩展 - 未捕获的TypeError:无效尝试传播不可迭代的实例 - Redux Web Extension - Uncaught TypeError: Invalid attempt to spread non-iterable instance 未处理的拒绝(TypeError):散布不可迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM