React-virtualized table 未呈现为 table

React-virtualized table not rendering as a table

我有一个基于 demo 编写的超级基本示例,但它不起作用:

import React from 'react';
import {
  Table,
  Column,
} from 'react-virtualized'

function MyTable(props) {
  return (
    <Table
      width={ 900 }
      height={ 500 }
      headerHeight={ 30 }
      rowHeight={ 30 }
      rowCount={ props.list.length }
      rowGetter={ ({ index }) => props.list[index] }
    >
      <Column
        width={ 250 }
        dataKey={ 'id' }
        headerRenderer={ ({ dataKey }) => 'Id' }
      />
      <Column
        width={ 250 }
        dataKey={ 'title' }
        headerRenderer={ ({ dataKey }) => 'Title' }
      />
    </Table>
  );
}

这是结果:

我确定我一定遗漏了什么,我遗漏了什么,为什么它没有显示为 table?

您没有导入 CSS。 Table 组件是唯一需要 CSS 设置 flexbox 布局样式的组件。

查看 the docs:

// Most of react-virtualized's styles are functional (eg position, size).
// Functional styles are applied directly to DOM elements.
// The Table component ships with a few presentational styles as well.
// They are optional, but if you want them you will need to also import the CSS file.
// This only needs to be done once; probably during your application's bootstrapping process.
import 'react-virtualized/styles.css'

// You can import any component you want as a named export from 'react-virtualized', eg
import { Column, Table } from 'react-virtualized'

如果需要,您还可以为 table 提供自己的样式。在你 link 回到你的 class 作为导入的 css 中,你可以像这样定义 react-virtualize 内置 classes:

:global {
  .ReactVirtualized__Table {

  }

  .ReactVirtualized__Table__headerColumn {

  }

  .ReactVirtualized__Table__headerRow {

  }

  .ReactVirtualized__Table__row {

  }

  .ReactVirtualized__Table__rowColumn {

  }
}

我建议使用 flex 来操纵 table 外观。希望对您有所帮助。