Skip to content
Grid LayoutGuides

Save and restore layouts

comins-grid-layout@0.3.0React

DashboardGrid computes positions when widgets move or resize. Connect onLayoutCommit to commands.applyLayoutSnapshot so React state reflects those positions before saving. Otherwise the saved state may differ from the visible layout.

  1. Save layout captures the full state in memory.
  2. Change width, or drag a widget title.
  3. Restore saved returns to the captured layout.

This example stores the snapshot in React memory. To persist across reloads, choose serializable data and write it to application storage.

Method Input / return Purpose
serializeLayout() Returns DashboardLayoutSnapshot Reads the current column count and widget geometry.
serializeState() Returns DashboardStateSnapshot<TData> Includes widget data, maximize/minimize restore positions, and visited column layouts.
applyLayoutSnapshot(snapshot) DashboardLayoutSnapshot → void Commits the grid geometry into state.
restoreLayout(snapshot) DashboardStateSnapshotInput<TData> → void Replaces state with the saved snapshot.
resetLayout(snapshot?) Geometry or state snapshot → void Resets to the initial state when omitted; otherwise replaces it with the snapshot.
Field Type Meaning
columns DashboardColumnCount Current column count
widgets DashboardWidget<TData>[] Current widgets, geometry, data, and state
previousLayouts Record<DashboardWidgetId, DashboardWidgetLayout> Geometry to restore from maximize/minimize
layoutsByColumn DashboardLayoutsByColumn Cached layouts for visited column counts

DashboardLayoutSnapshot.widgets is DashboardWidgetLayout[], not complete widgets. The two snapshots are not interchangeable.

For backward compatibility, restoreLayout accepts inputs without previousLayouts or layoutsByColumn. When the active column cache differs from top-level state, top-level widgets and previousLayouts take priority. Outside a React reducer, the pure serializeDashboardState helper is also available.

Storage, schema migration, and server synchronization belong to the consumer application. Validate external JSON types, IDs, geometry, and business data before restoring. TypeScript annotations are not runtime JSON validation.

Save layout, persist state, restore, persistence, serializeState, restoreLayout.

Version-pinned persistence guide · npm 0.3.0