State Persistence
State persistence
Save and restore the user's grid state with persistState, read and apply it with getState and restoreState, reset it, share it as a URL-safe string, and keep named saved views.
persistState saves what the user did to the grid and restores it the next
time the grid mounts:
<DataGrid id="orders" persistState data={orders} columns={columns} sorting filtering paging />
With true the state goes to localStorage under the key
salmon-data-grid:<id>, so the grid needs a stable id (without one a
development warning explains that nothing is persisted). Saves are
coalesced: a burst of changes writes once.
What is persisted
The state (DataGridState) has one part per feature (DataGridStatePart):
| Part | Contents |
|---|---|
columns |
Column order, widths and visibility |
pinning |
Columns pinned at runtime |
sorting |
Sorted columns and directions |
filters |
Filter conditions per column |
grouping |
Grouped columns |
paging |
Page and page size |
search |
Search text and options |
pinnedRows |
Rows pinned at runtime |
tree |
Tree mode expansion |
selection |
Selected rows |
scroll |
Scroll position |
Columns are referenced by field or column id; a stored column that no longer exists is dropped and reported instead of failing the restore. Templates and other definition-time settings are never stored: they come from the props.
Options
An object configures the binding:
<DataGrid
id="orders"
persistState={{
key: 'orders-view', // default `salmon-data-grid:<id>`
storage: myStorage, // default localStorage
exclude: ['selection', 'scroll'],
version: 3,
migrate: (state, version) => (version === 2 ? upgrade(state) : null),
}}
/>
storageis any object withload(key),save(key, state)and an optionalremove(key); each may be synchronous or return a promise, sosessionStorage, IndexedDB or a server endpoint fit.@salmonui/datacreateKeyValueStateStorageandcreateJsonStateStoragebuild such storages over a key-value store or the JSON protocol endpoint.excludeleaves parts out of saving and restoring.versionis the application state version. A state saved by another version is discarded, unlessmigrateupgrades it; returningnullfrommigratediscards it too. The state itself also carries a schema version, so a state from an older Salmon UI release is upgraded or rejected rather than half applied.
SSR and hydration
Storage is read only in the browser, after mount. The server and the hydrating client both render the definition state, so there is no hydration mismatch; the stored state is restored right after hydration, in the same task, before the browser paints.
The state API
The same state is available through the grid instance (ref or
useDataGridApi()):
api.getState(options?)returns the currentDataGridState, a plain JSON object;{ exclude }leaves parts out.api.restoreState(stateOrString, options?)applies a state in one batch without recompiling columns. It returns{ status: 'applied' | 'partial', ignoredColumns, rejected, skipped, scroll }:partiallists columns that no longer exist, commands the grid rejected, and parts whose feature is not enabled.api.resetState()returns to the definition state (the props the grid was created with) and, whenpersistStateis on, clears the stored entry.
serializeDataGridState(state) and deserializeDataGridState(text) from
@salmonui/data-grid convert a state to and from a compact, URL-safe
base64url string, for example to share a view in a link:
const url = `${location.pathname}?view=${serializeDataGridState(api.getState({ exclude: ['selection', 'scroll'] }))}`;
// Later: api.restoreState(new URLSearchParams(location.search).get('view') ?? '');
restoreState accepts the serialized string directly.
Named saved views
Named views sit on top of the same state API: createSavedViews from @salmonui/data/views
keeps a list of named DataGridState values for one grid in any key-value store
(localStorage, sessionStorage, an IndexedDB wrapper, a server-backed
store). The grid never imports it, so it adds nothing to a grid that does
not use it.
import { createSavedViews } from '@salmonui/data/views';
// In an effect or event handler (localStorage does not exist on the server):
const views = createSavedViews(localStorage, { gridId: 'orders' });
// Save the current layout under a name (an existing `id` replaces that view).
await views.save({ name: 'Open orders', state: api.getState({ exclude: ['selection', 'scroll'] }) });
// List, apply, rename and delete.
const list = await views.list(); // [{ id, name, description?, state, createdAt, updatedAt, schemaVersion }]
api.restoreState(list[0].state);
await views.rename(list[0].id, 'Unshipped');
await views.remove(list[0].id);
- A view is
{ id, name, description?, state, createdAt, updatedAt, schemaVersion }. All views of a grid are stored as one JSON document undersalmonui:grid-views:<gridId>(prefixchanges the key). subscribe(listener)reports the list after every change, so a view picker stays in sync; writes are queued, so two quick saves never drop each other.migrate(record, schemaVersion)upgrades records written by an older format or drops them by returningnull. The grid state inside a view keeps its own schema version, whichrestoreStatechecks as usual.- Applying a view is an ordinary
restoreState: one batch, no column recompile, and the sameRestoreResult. Combine it withpersistStateto also remember the last layout the user had.
Named saved views
Open demoSave the current sort, filters, grouping, page and column layout under a name, switch between saved views, and delete them; views are stored in localStorage with createSavedViews and applied with restoreState.
Sort, filter, group or resize, then save the layout as a named view.
Restoring keeps the compiled columns
restoreState and resetState apply column order, widths, visibility and
pinning as runtime commands on the existing columns; the column definitions
are never recompiled, so templates, formatters and lookups stay in place and
the restore costs one notification. The demo below checks it:
api.getColumn(field) returns the same compiled column before and after a
restore.
Controlled state
State persistence is optional. An application that owns the view (in a URL,
a store or a server) can control it entirely through props, the additive
counterpart of getState() / restoreState():
| State | Prop | Change event |
|---|---|---|
| Sort | sorting.sortBy |
onSortChange |
| Filters | filtering.filters |
onFilterChange |
| Search | search.text |
onSearchChange |
| Page | paging.page, paging.pageSize |
onPageChange |
| Grouping | grouping.groupBy |
onGroupChange |
| Column visibility, width, pinning, order | column hidden, width, pinned, array order |
onColumnVisibilityChange, onColumnResizeEnd, onColumnPinChange, onColumnReorder |
| Selection | selection.rowIds |
onSelectionChange |
Options props are compared structurally with their previous value, so an
object recreated with the same content is a no-op, and a changed value is
applied as a command (not a remount). Writing the event payload back into
your state closes the loop. Every change event carries source
('pointer', 'keyboard', 'touch' or 'api'), and restoreState raises the same
events, so a restored snapshot reaches the application state too. Keep
scroll positions, hover and drag state out of React state: the grid owns
those transients.
Controlled grid state
Open demoThe application owns the sort, filters, search, page, hidden columns and selection through reactive props and change events, takes a getState() snapshot and restores it without recompiling columns.
Sort, filter, search, page, hide or select in the grid, or use the buttons: both sides stay in sync.
{
"sortBy": [
{
"field": "amount",
"direction": "desc"
}
],
"filters": [],
"search": "",
"page": 1,
"hidden": [],
"selected": []
}State persistence
Open demoA grid with an id and persistState saves sorting, filters, paging, column order and widths to localStorage and restores them after a reload; buttons read, restore, reset and serialize the state.
Sort, filter, page, resize or drag a column, then reload the page: the grid comes back as you left it.
API reference
See DataGridPersistState,
DataGridState,
RestoreResult and
createSavedViews in the API
reference.