Skip to main content

State management

Grid state is one object, QueryState, with six parts: paginationModel, sortModel, filterModel, searchText, columnVisibilityModel, and selectionModel. You choose how much of it to own.

Three levels of control​

ApproachPropsUse it when
UncontrolledinitialStateThe grid can own its state. initialState is read once.
Whole statestate, onStateChangeOne screen owns everything, as in server mode.
Per modelsortModel, filterModel, searchText, paginationModel, columnVisibilityModel, selectionModel, each with an on…Change callbackYou only need to own one part.

state is partial: only the keys you include are controlled, and the rest stay internal. An individual prop such as sortModel wins over the same key in state.

Example​

This screen controls only the filter, so a saved filter can be applied from outside the grid.

controlled-state.tsx
import { useState } from 'react';
import { Pressable, Text, View } from 'react-native';
import { DataGrid, type FilterModel } from 'expo-flash-datagrid';

import { userColumns, users, type User } from './data';

const activeAdults: FilterModel = {
items: [{ id: 'adults', field: 'age', operator: '>=', value: 30 }],
logicOperator: 'and',
};

const noFilters: FilterModel = { items: [], logicOperator: 'and' };

export function SavedFilterScreen() {
const [filterModel, setFilterModel] = useState<FilterModel>(noFilters);

return (
<View style={{ flex: 1 }}>
<Pressable onPress={() => setFilterModel(activeAdults)}>
<Text>Saved filter: age 30+</Text>
</Pressable>

<DataGrid<User>
mode="client"
rows={users}
columns={userColumns}
filterModel={filterModel}
onFilterModelChange={setFilterModel}
initialState={{ sortModel: [{ field: 'name', sort: 'asc' }] }}
style={{ flex: 1 }}
/>
</View>
);
}

Change callbacks​

onStateChange and onQueryChange both fire with the full next QueryState, and only when something actually changed. The per-model callbacks fire first, for just the models that changed.

Changing the sort, filters, or search resets the page to 0.