Skip to content

Persist layout (AsyncStorage / MMKV)

useTable reports layout changes through onPreferencesChange and accepts them back through initialPreferences or setPreferences — see Persisting layout.

Works in Expo Go. Storage is asynchronous, so restore with setPreferences once it loads:

Terminal window
npx expo install @react-native-async-storage/async-storage
usePersistentTable.ts
import { useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useTable, type Column, type TablePreferences, type TableRow } from 'expo-modern-table';
export function usePersistentTable<T extends TableRow>(
storageKey: string,
data: T[],
columns: Column<T>[]
) {
const table = useTable(data, columns, {
onPreferencesChange: prefs => {
AsyncStorage.setItem(storageKey, JSON.stringify(prefs)).catch(() => {});
},
});
const { setPreferences } = table;
useEffect(() => {
AsyncStorage.getItem(storageKey).then(saved => {
if (saved) setPreferences(JSON.parse(saved) as Partial<TablePreferences>);
});
}, [storageKey, setPreferences]);
return table;
}

onPreferencesChange also fires on mount with the defaults; if your columns load slowly, you may want to skip that first write.

Use one storage key per table ('orders-table', 'team-table'). Because visibility and pinning are stored as overrides of each column’s defaults, adding or removing columns in a later app version is safe — new columns show up with their defaults and unknown keys are ignored.