我正在create-react-app项目(版本^7.0.25
)中尝试react-table。我使用的是他们快速入门documentation中的确切示例。但是,我在访问器和数据列之间遇到了类型错误。下面是我的代码,
import * as React from 'react';
import { Solution } from '../../../Models/SolutionModel';
import {useTable} from 'react-table'
interface ICompareTableComponentProps {
Solutions : Solution[]
}
const CompareTableComponent: React.FunctionComponent<ICompareTableComponentProps> = (props) => {
const data = React.useMemo(
() => [
{
col1: 'Hello',
col2: 'World',
},
{
col1: 'react-table',
col2: 'rocks',
},
{
col1: 'whatever',
col2: 'you want',
},
],
[]
)
const columns = React.useMemo(
() => [
{
Header: 'Column 1',
accessor: 'col1', // accessor is the "key" in the data
},
{
Header: 'Column 2',
accessor: 'col2',
},
],
[]
)
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({ columns, data })
return (
<table {...getTableProps()} style={{ border: 'solid 1px blue' }}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th
{...column.getHeaderProps()}
style={{
borderBottom: 'solid 3px red',
background: 'aliceblue',
color: 'black',
fontWeight: 'bold',
}}
>
{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()}
style={{
padding: '10px',
border: 'solid 1px gray',
background: 'papayawhip',
}}
>
{cell.render('Cell')}
</td>
)
})}
</tr>
)
})}
</tbody>
</table>
)
};
export default CompareTableComponent;
下面是我的全部错误,
Type '{ Header: string; accessor: string; }[]' is not assignable to type 'Column<{ col1: string; col2: string; }>[]'.
Type '{ Header: string; accessor: string; }' is not assignable to type 'Column<{ col1: string; col2: string; }>'.
Type '{ Header: string; accessor: string; }' is not assignable to type 'ColumnInterface<{ col1: string; col2: string; }> & { accessor: "col2"; } & ColumnInterfaceBasedOnValue<{ col1: string; col2: string; }, string>'.
Type '{ Header: string; accessor: string; }' is not assignable to type '{ accessor: "col2"; }'.
Types of property 'accessor' are incompatible.
Type 'string' is not assignable to type '"col2"'. TS2322
我尝试过将列转换为对象,但没有成功。
{
col1: 'react-table' as any,
col2: 'rocks' as any,
}
我确信我忽略了一些非常琐碎的事情,但似乎找不到问题所在。在这方面的任何帮助都将非常感激。
3条答案
按热度按时间brtdzjyr1#
您的
useMemo
缺少类型waxmsbnn2#
在此处查看此主题:https://github.com/ggascoigne/react-table-example/issues/3
基本上,您需要定义:
..或使用数据行的接口:
eni9jsuy3#
对我有用的东西:
);