React表未通过foreach错误加载

发布于 2025-01-24 11:16:32 字数 2768 浏览 0 评论 0原文

我遇到了一个错误的错误,即“未定义的typeError:无法读取未定义的属性(读取'foreach')”

看来它位于我的桌子组件中。

这是我的桌子组件的代码。


import React from 'react';
import { useTable } from 'react-table';

export interface TableProps {
    columns: any;
    data: any
};

const TableOne:React.FC<TableProps> = ({columns, data}) => {
   

    const tableInstance = useTable({columns, data});

    const {getTableProps, getTableBodyProps, headerGroups, rows, prepareRow} = tableInstance;

  // Render the UI for your table
  return (
    <table {...getTableProps()} >
      <thead>
        {headerGroups.map((headerGroup) => (
          <tr {...headerGroup.getHeaderGroupProps()}>
            {headerGroup.headers.map((column) => (
              <th {...column.getHeaderProps()}>{column.render("Header")}</th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody {...getTableBodyProps()}>
        {rows.map((row, i) => {
          prepareRow(row);
          return (
            <tr {...row.getRowProps()}>
              {row.cells.map((cell) => {
                return <td {...cell.getCellProps()}>{cell.render("Cell")}</td>;
              })}
            </tr>
          );
        })}
      </tbody>
    </table>
  );
}

export default TableOne

如您所见,我只是使其成为功能性组件,并且可以将道具发送到组件中,以便将其用于多个数据集。这是React-Table站点上的模板代码中的副本和粘贴代码。

在我的主页中,我有以下代码。


import React, {useMemo} from 'react'
import Table from '../components/Table'
import {gql, useQuery} from "@apollo/client"
import TableOne from '../components/TableOne'

const style ={
    hero:`bg-green-500 w-full h-96 p-2`
}

const AllCategoriesQuery = gql`
  query {
    allCategories { 
      name
  }
  }
` 

const Home = () => {

  const {data, loading, error} = useQuery(AllCategoriesQuery, {
    onCompleted: data => {
        console.log(data)
    }
})


  const columns = useMemo(() => [
    {
      Header: "Name",
      accessor: "name"
    },
  ], [data])
   
   
  return (
    <div className={style.hero}>
        Home123
        {/* <Table /> */}
        
        <TableOne columns={columns} data={data}  />
        
    </div>
  )
}

export default Home

我只是简单地将我的Apollo-client数据越过了,但是当我运行它时,我的整个应用程序都会消失。它不仅会使页面消失,还可以使此页面之外的其他组件。

任何帮助都很棒!

编辑:错误的屏幕截图

“在此处输入图像描述”

”在此处输入图像描述”

I am running into an error of which is "Uncaught TypeError: Cannot read properties of undefined (reading 'forEach')"

It seems it is within my TableOne component.

Here is the code for my TableOne component.


import React from 'react';
import { useTable } from 'react-table';

export interface TableProps {
    columns: any;
    data: any
};

const TableOne:React.FC<TableProps> = ({columns, data}) => {
   

    const tableInstance = useTable({columns, data});

    const {getTableProps, getTableBodyProps, headerGroups, rows, prepareRow} = tableInstance;

  // Render the UI for your table
  return (
    <table {...getTableProps()} >
      <thead>
        {headerGroups.map((headerGroup) => (
          <tr {...headerGroup.getHeaderGroupProps()}>
            {headerGroup.headers.map((column) => (
              <th {...column.getHeaderProps()}>{column.render("Header")}</th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody {...getTableBodyProps()}>
        {rows.map((row, i) => {
          prepareRow(row);
          return (
            <tr {...row.getRowProps()}>
              {row.cells.map((cell) => {
                return <td {...cell.getCellProps()}>{cell.render("Cell")}</td>;
              })}
            </tr>
          );
        })}
      </tbody>
    </table>
  );
}

export default TableOne

As you can see, I am just making it a functional component and having props being able to be sent into the component so that it can be used for multiple datasets. This is the copy and paste code from the template code on the React-Table site.

In my Home Page I have the following code.


import React, {useMemo} from 'react'
import Table from '../components/Table'
import {gql, useQuery} from "@apollo/client"
import TableOne from '../components/TableOne'

const style ={
    hero:`bg-green-500 w-full h-96 p-2`
}

const AllCategoriesQuery = gql`
  query {
    allCategories { 
      name
  }
  }
` 

const Home = () => {

  const {data, loading, error} = useQuery(AllCategoriesQuery, {
    onCompleted: data => {
        console.log(data)
    }
})


  const columns = useMemo(() => [
    {
      Header: "Name",
      accessor: "name"
    },
  ], [data])
   
   
  return (
    <div className={style.hero}>
        Home123
        {/* <Table /> */}
        
        <TableOne columns={columns} data={data}  />
        
    </div>
  )
}

export default Home

I just simply past my apollo-client data into the prop of the , however when I run this it makes my whole app disappear. It doesn't just make the page go away but also other components that are outside of this page.

Any assistance would be great!

Edit: Screenshot of error

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2025-01-31 11:16:32

尝试以下操作:

return (
    <div className={style.hero}>
        Home123
        {/* <Table /> */}
        
        {data && <TableOne columns={columns} data={data}  /> }
        
    </div>
  )

Try this:

return (
    <div className={style.hero}>
        Home123
        {/* <Table /> */}
        
        {data && <TableOne columns={columns} data={data}  /> }
        
    </div>
  )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文