State and persistence
Mosyn owns the live layout while a grid is mounted. Use useGridStore to observe that layout from application UI, then persist the tile data wherever your product stores user preferences.
Read the current tiles
Section titled “Read the current tiles”Call useGridStore inside the shared provider to subscribe to the root grid’s live tile array.
import { useGridStore } from 'mosyn';
export function SaveLayoutButton() { const tiles = useGridStore((state) => state.tiles);
return ( <button type="button" onClick={() => saveDashboardLayout(tiles ?? [])}> Save layout </button> );}The value updates as the grid changes, so your save action always receives the current arrangement.
Save only what your product needs
Section titled “Save only what your product needs”Most applications should persist the structural tile fields plus their own data payload. Keep rendered React content in application code rather than attempting to serialize it.
export type SavedTile = { id: string; x: number; y: number; w: number; h: number; data?: unknown;};
export function toSavedTiles(tiles: Tile[]): SavedTile[] { return tiles.map(({ id, x, y, w, h, data }) => ({ id, x, y, w, h, data }));}When loading a saved layout, combine the saved geometry with the renderer defined by your application.
export const tiles: Tile[] = savedTiles.map((saved) => ({ ...saved, renderer: tileRenderers[saved.id],}));Read a nested grid
Section titled “Read a nested grid”Pass the container tile id as the second argument to useGridStore when you need the layout inside one nested grid.
const workspaceTiles = useGridStore((state) => state.tiles, 'workspace');Use this for workspace-level counters, save actions, or summaries outside the container itself.
Reset a layout intentionally
Section titled “Reset a layout intentionally”To discard the live layout and begin again with a fresh tile array, remount the provider with a new key.
const [version, setVersion] = useState(0);const tiles = useMemo(() => createTiles(), [version]);
return ( <MosynManager key={version}> <Mosyn tiles={tiles} /> <button type="button" onClick={() => setVersion((value) => value + 1)}> Reset layout </button> </MosynManager>);This creates a new interaction manager and seeds the grid from the new tile array.