开发者问题收集

react-table 未捕获的类型错误:headerGroup.map 不是一个函数

2022-06-30
1338

我正在使用 javascript 编写 react-table,但是我在传递到表函数的数据时收到错误:

tableee.js:25 Uncaught TypeError: headerGroup.map 不是函数

在 tableee.js:25:1

在 Array.map ()

在 Tableee (tableee.js:23:1)

在 renderWithHooks (react-dom.development.js:16305:1)

在 mountIndeterminateComponent (react-dom.development.js:20074:1)

在 beginWork (react-dom.development.js:21587:1)

在 HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)

在 Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)

在invokeGuardedCallback(react-dom.development.js:4277:1)

在beginWork$1(react-dom.development.js:27451:1)

Tablee.js

import React, { useMemo } from "react";
import { useTable } from "react-table";
import MOCK_DATA from "./MOCK_DATA.json";
import { COLUMNS } from "./columns";
import "./tableee.css";

const Tableee = () => {
  const columns = useMemo(() => COLUMNS, []);
  const data = useMemo(() => MOCK_DATA, []);
  const tableInstance = useTable({
    columns: columns,
    data: data,
  });
  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
  } = tableInstance;
  return (
    <table {...getTableProps()}>
      <thead>
        {headerGroups.map((headerGroup) => (
          <tr {...headerGroup.getHeaderGroupProps()}>
            {headerGroup.map((column) => (
              <th {...column.getHeaderProps()}>{column.render("Header")}</th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody {...getTableBodyProps()}>
        {rows.map((row) => {
          prepareRow(row);
          return (
            <tr {...row.getRowProps()}>
              {row.cells.map((cell) => {
                return <td {...cell.getCellProps()}>{cell.render("cell")}</td>;
              })}
            </tr>
          );
        })}
      </tbody>
    </table>
  );
};

export default Tableee;

columns.js

export const COLUMNS = [
  {
    Header: "No",
    accessor: "_id",
  },
  {
    Header: "Project Name",
    accessor: "Project_name",
  },
  {
    Header: "Customer Name",
    accessor: "customer_name",
  },
  {
    Header: "Status",
    accessor: "status",
  },
  {
    Header: "Archived",
    accessor: "archived",
  },
];

请帮帮我

1个回答

使用 map 前需要检查 headerGroup 数据是否有。

{headerGroups && headerGroups.map((headerGroup) => (
   <tr {...headerGroup.getHeaderGroupProps()}>
      {headerGroup && headerGroup.map((column) => (
         <th {...column.getHeaderProps()}>{column.render("Header")}</th>
       ))}
    </tr>
))}
Ahmad
2022-06-30