React表未通过foreach错误加载
我遇到了一个错误的错误,即“未定义的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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作:
Try this: