【问题标题】:react-table pagination properties doesn't exist on type 'TableInstance{}''TableInstance{}' 类型上不存在 react-table 分页属性
【发布时间】:2021-02-12 22:49:35
【问题描述】:

我正在尝试使用 react-table 实现分页。在大多数教程中,我们都是这样开始的:

const {
        getTableProps,
        getTableBodyProps,
        headerGroups,
        page,
        prepareRow,
        visibleColumns,
        canPreviousPage,
        canNextPage,
        pageOptions,
        pageCount,
        gotoPage,
        nextPage,
        previousPage,
        setPageSize,
        state: { pageIndex, pageSize },
    } = useTable(
        {
            columns,
            data,
            defaultColumn: { Filter: DefaultColumnFilter },
            initialState: { pageIndex: 0, pageSize: 10 },
        },

在我的情况下,我的 IDE 说,属性: 页, 可以上一页, 可以下一页, 页面选项, 页数, 转到页面, 下一页, 上一页, 设置页面大小

在类型“TableInstance{}”上不存在。

在 www 中进行了大量研究后,我找不到这些属性不在 TableInstance 中的原因。 如果没有这些属性,我如何使用分页,或者有没有办法让它工作?

期待每一个答案

【问题讨论】:

  • 你能分享一个你正在使用的教程的链接吗?
  • 主要问题是TS类型不对!如果您检查@types/react-table 中的index.d.ts 文件,您会看到,例如,setPageSize 属性仅在UsePaginationInstanceProps 中声明,而UsePaginationInstanceProps 未在其他地方使用。
  • 可能,这个包的维护者在包本身被移动到新版本时还没有更新类型。

标签: reactjs typescript pagination frontend react-table


【解决方案1】:

创建一个包含以下内容的react-table-config.d.ts 文件为我解决了这个问题:

import {
  UseColumnOrderInstanceProps,
  UseColumnOrderState,
  UseExpandedHooks,
  UseExpandedInstanceProps,
  UseExpandedOptions,
  UseExpandedRowProps,
  UseExpandedState,
  UseFiltersColumnOptions,
  UseFiltersColumnProps,
  UseFiltersInstanceProps,
  UseFiltersOptions,
  UseFiltersState,
  UseGlobalFiltersColumnOptions,
  UseGlobalFiltersInstanceProps,
  UseGlobalFiltersOptions,
  UseGlobalFiltersState,
  UseGroupByCellProps,
  UseGroupByColumnOptions,
  UseGroupByColumnProps,
  UseGroupByHooks,
  UseGroupByInstanceProps,
  UseGroupByOptions,
  UseGroupByRowProps,
  UseGroupByState,
  UsePaginationInstanceProps,
  UsePaginationOptions,
  UsePaginationState,
  UseResizeColumnsColumnOptions,
  UseResizeColumnsColumnProps,
  UseResizeColumnsOptions,
  UseResizeColumnsState,
  UseRowSelectHooks,
  UseRowSelectInstanceProps,
  UseRowSelectOptions,
  UseRowSelectRowProps,
  UseRowSelectState,
  UseRowStateCellProps,
  UseRowStateInstanceProps,
  UseRowStateOptions,
  UseRowStateRowProps,
  UseRowStateState,
  UseSortByColumnOptions,
  UseSortByColumnProps,
  UseSortByHooks,
  UseSortByInstanceProps,
  UseSortByOptions,
  UseSortByState
} from 'react-table'

declare module 'react-table' {
  // take this file as-is, or comment out the sections that don't apply to your plugin configuration

  export interface TableOptions<D extends Record<string, unknown>>
    extends UseExpandedOptions<D>,
      UseFiltersOptions<D>,
      UseGlobalFiltersOptions<D>,
      UseGroupByOptions<D>,
      UsePaginationOptions<D>,
      UseResizeColumnsOptions<D>,
      UseRowSelectOptions<D>,
      UseRowStateOptions<D>,
      UseSortByOptions<D>,
      // note that having Record here allows you to add anything to the options, this matches the spirit of the
      // underlying js library, but might be cleaner if it's replaced by a more specific type that matches your
      // feature set, this is a safe default.
      Record<string, any> {}

  export interface Hooks<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseExpandedHooks<D>,
      UseGroupByHooks<D>,
      UseRowSelectHooks<D>,
      UseSortByHooks<D> {}

  export interface TableInstance<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseColumnOrderInstanceProps<D>,
      UseExpandedInstanceProps<D>,
      UseFiltersInstanceProps<D>,
      UseGlobalFiltersInstanceProps<D>,
      UseGroupByInstanceProps<D>,
      UsePaginationInstanceProps<D>,
      UseRowSelectInstanceProps<D>,
      UseRowStateInstanceProps<D>,
      UseSortByInstanceProps<D> {}

  export interface TableState<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseColumnOrderState<D>,
      UseExpandedState<D>,
      UseFiltersState<D>,
      UseGlobalFiltersState<D>,
      UseGroupByState<D>,
      UsePaginationState<D>,
      UseResizeColumnsState<D>,
      UseRowSelectState<D>,
      UseRowStateState<D>,
      UseSortByState<D> {}

  export interface ColumnInterface<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseFiltersColumnOptions<D>,
      UseGlobalFiltersColumnOptions<D>,
      UseGroupByColumnOptions<D>,
      UseResizeColumnsColumnOptions<D>,
      UseSortByColumnOptions<D> {}

  export interface ColumnInstance<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseFiltersColumnProps<D>,
      UseGroupByColumnProps<D>,
      UseResizeColumnsColumnProps<D>,
      UseSortByColumnProps<D> {}

  export interface Cell<D extends Record<string, unknown> = Record<string, unknown>, V = any>
    extends UseGroupByCellProps<D>,
      UseRowStateCellProps<D> {}

  export interface Row<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseExpandedRowProps<D>,
      UseGroupByRowProps<D>,
      UseRowSelectRowProps<D>,
      UseRowStateRowProps<D> {}
}

如建议: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-table

【讨论】:

  • 非常感谢 :)
  • OP 和@Muzaffer,我已经在@types/react-table 下创建了文件,但问题仍然存在,只创建这个文件就够了吗?
  • 你把这个文件放在哪里了?你的项目还是 node_modules/@types/react-table?
  • 这个文件应该放在哪里?
  • 它应该可以在您的项目可以检测到文件的任何地方工作。文档建议将其放在根目录或类型文件夹中
【解决方案2】:

这也许不是真正的“最佳”答案。但是,如果您想运行npm build,则不能除非您“修复”丢失的类型错误。我所做的只是将@ts-ignore 放在带下划线的代码段上方。它是这样的:

    // @ts-ignore
    nextPage

【讨论】:

    猜你喜欢
    • 2022-10-14
    • 2020-10-04
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 2020-05-15
    • 1970-01-01
    • 2023-01-09
    相关资源
    最近更新 更多