Data Table
React Native (Expo)
A React Native table with sortable columns, a header that stays put while rows scroll and sideways scrolling for wide columns.
A table that scrolls sideways when its columns are wider than the screen and keeps its header visible while the rows scroll up and down. Tapping a sortable header sorts by it, then reverses, and the arrow turns to match. Headers report their sort order to screen readers. Built for React Native and Expo with react-native only; this page previews it in the browser through react-native-web.
Installation
CLI
npx @modus-ui/cli add data-tableManual
- Copy the code from the component tab.
- Create a file at
components/ui/data-table.tsx. - Paste the code.
It needs no packages beyond react-native, which your Expo or React Native app already has.
Usage
tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
columns | DataTableColumn<T>[] | - | Each column's key, title, and optional width, align, sortable and render. |
rows | T[] | - | The data to show. |
rowKey | (row: T) => string | - | Returns a unique key for a row. |
initialSort | { key: string; direction: "asc" | "desc" } | - | Column key and direction to sort by at first. |
onRowPress | (row: T) => void | - | Called when a row is pressed. |
striped | boolean | true | Shades every other row. |
maxHeight | number | 280 | Height of the rows area; the header stays above it. |
colors | Partial<DataTableColors> | - | Overrides header, headerText, text, border, stripe and background. |
style | StyleProp<ViewStyle> | - | Style for the table's frame. |
Comments