typescript React数据表-属性'accessor'的型别不相容

yr9zkbsy  于 2022-11-18  发布在  TypeScript
关注(0)|答案(3)|浏览(206)

我正在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,
}

我确信我忽略了一些非常琐碎的事情,但似乎找不到问题所在。在这方面的任何帮助都将非常感激。

brtdzjyr

brtdzjyr1#

您的useMemo缺少类型

const columns = useMemo<Column<{col1: string}>[]>(() => [
  {
    col1: 'hello world',
  },
], [])
waxmsbnn

waxmsbnn2#

在此处查看此主题:https://github.com/ggascoigne/react-table-example/issues/3
基本上,您需要定义:

const columns: Array<Column<{ col1: string; col2: string }>> = useMemo(

..或使用数据行的接口:

const columns: Array<Column<DataInterface>> = ...
eni9jsuy

eni9jsuy3#

对我有用的东西:

  • 已创建接口
interface Data {
 id: number;
 first_name: string;
 last_name: string;
 email: string;
 date_of_birth: string;
 age: number;
 country: string;
 phone: string
}
  • 定义列,如下所示:
const columns = React.useMemo<Column<Data>[]>(
 () => [
     { Header: "ID", accessor: "id" },
     { Header: "First name", accessor: "first_name" },
     { Header: "Last name", accessor: "last_name" },
     { Header: "Email", accessor: "email" },
     { Header: "Date of Birth", accessor: "date_of_birth" },
     { Header: "Age", accessor: "age" },
     { Header: "Country", accessor: "country" },
     { Header: "Phone", accessor: "phone" },
 ],
 []

);

相关问题