Custom cells
Change what a cell shows with a derived value, a formatted string, or your own component.
Basic usage
custom-cells.tsx
import { StyleSheet, Text, View } from 'react-native';
import { DataGrid, type ColumnDef } from 'expo-flash-datagrid';
import { users, type User, type UserStatus } from './data';
const STATUS_COLORS: Record<UserStatus, string> = {
new: '#2563EB',
active: '#16A34A',
paused: '#D97706',
};
const columns: ColumnDef<User>[] = [
{ field: 'name', headerName: 'Name', flex: 1, minWidth: 160 },
{
field: 'location',
headerName: 'Location',
width: 170,
valueGetter: (row) => `${row.city} (${row.age})`,
},
{
field: 'salary',
headerName: 'Salary',
type: 'number',
width: 120,
align: 'right',
headerAlign: 'right',
valueFormatter: (value) =>
`$${Number(value ?? 0).toLocaleString('en-US')}`,
},
{
field: 'status',
headerName: 'Status',
width: 120,
renderCell: ({ row }) => (
<View
style={[
styles.badge,
{ backgroundColor: STATUS_COLORS[row.status] },
]}
>
<Text style={styles.badgeLabel}>{row.status.toUpperCase()}</Text>
</View>
),
renderHeader: ({ column, sortDirection }) => (
<Text style={styles.header}>
{column.headerName}
{sortDirection ? ` (${sortDirection})` : ''}
</Text>
),
},
];
const styles = StyleSheet.create({
badge: {
alignSelf: 'flex-start',
borderRadius: 999,
paddingHorizontal: 9,
paddingVertical: 3,
},
badgeLabel: { color: '#FFFFFF', fontSize: 10, fontWeight: '800' },
header: { fontWeight: '700' },
});
export function CustomCellsScreen() {
return (
<DataGrid<User>
mode="client"
rows={users}
columns={columns}
style={{ flex: 1 }}
/>
);
}
Options
| Field | Type | Description |
|---|---|---|
valueGetter | (row) => unknown | Computes the cell value. Sort, filter, and search use this value. |
valueFormatter | (value, row) => string | Formats the value for display. Global search also matches this text. |
renderCell | (params: CellRenderParams) => ReactNode | Replaces the cell's content. |
renderHeader | (params: HeaderRenderParams) => ReactNode | Replaces the header's content. |
renderCell receives row, value, rowId, field, rowIndex, column, and isSelected. renderHeader receives column and sortDirection.
Client vs server
These options only affect rendering in server mode. In client mode, valueGetter also feeds the sort, filter, and search steps.