createPersister()
Creates a persister instance for saving and restoring flow state. Adds features like TTL, versioning, and validation on top of storage stores.
Import
Section titled “Import”import { createPersister } from "@useflow/react";Signature
Section titled “Signature”function createPersister(options: PersisterOptions): FlowPersisterParameters
Section titled “Parameters”options
Section titled “options”Configuration object for the persister:
type PersisterOptions = { store: FlowStore; // Required: Storage backend ttl?: number; // Optional: Time to live in ms validate?: (state: PersistedFlowState) => boolean; // Optional: State validation onSave?: (flowId: string, state: PersistedFlowState) => void; // Optional: Save callback onRestore?: (flowId: string, state: PersistedFlowState) => void; // Optional: Restore callback onDiscard?: (event: FlowRestoreDiscardEvent) => void; // Optional: Discard callback onError?: (error: Error) => void; // Optional: Error handler (see below)}Storage stores
Section titled “Storage stores”Built-in stores
Section titled “Built-in stores”import { createLocalStorageStore, createSessionStorageStore, createMemoryStore} from "@useflow/react";
// Browser localStorageconst localStore = createLocalStorageStore();
// Browser sessionStorageconst sessionStore = createSessionStorageStore();
// In-memory (for testing)const memoryStore = createMemoryStore();Custom store interface
Section titled “Custom store interface”type FlowStore = { get: (flowId: string, options?: FlowStoreOptions) => Promise<PersistedFlowState | null> | PersistedFlowState | null; set: (flowId: string, state: PersistedFlowState, options?: FlowStoreOptions) => Promise<void> | void; remove: (flowId: string, options?: FlowStoreOptions) => Promise<void> | void; removeFlow?: (flowId: string) => Promise<void> | void; removeAll?: () => Promise<void> | void;}
type FlowStoreOptions = { instanceId?: string; variantId?: string;}Examples
Section titled “Examples”Basic persistence
Section titled “Basic persistence”import { createPersister, createLocalStorageStore } from "@useflow/react";
const persister = createPersister({ store: createLocalStorageStore()});
// Use with Flow component<Flow flow={myFlow} persister={persister} initialContext={{}}> {({ renderStep }) => renderStep({ /* ... */ })}</Flow>With ttl (time to live)
Section titled “With ttl (time to live)”const persister = createPersister({ store: createLocalStorageStore(), ttl: 7 * 24 * 60 * 60 * 1000 // 7 days});
// State older than 7 days will be ignoredWith custom validation
Section titled “With custom validation”const persister = createPersister({ store: createLocalStorageStore(), validate: (state) => { // Ensure required fields exist return state?.stepId && state?.context?.userId; }});
// Invalid states will be ignored on restoreWith callbacks
Section titled “With callbacks”const persister = createPersister({ store: createLocalStorageStore(), onSave: (flowId, state) => { console.log(`Saved ${flowId} at step ${state.stepId}`); }, onRestore: (flowId, state) => { console.log(`Restored ${flowId} from step ${state.stepId}`); }, onError: (error) => { console.error('Persistence error:', error); }});onError
Section titled “onError”Called when a store read, write or removal fails, or when a persister callback throws. The operation still resolves (save and restore return null), so failures never break the flow.
save(), restore() and remove() also accept an onError option for one call. It replaces the persister’s onError rather than adding to it. <Flow> passes one whenever onPersistenceError is set, so failures reach that handler with the flow ID and operation, and the persister’s onError does not also fire. Without a Flow or provider handler, the persister’s onError runs as usual.
Built-in key-value stores throw when stored data can’t be read or deserialized, so corrupted state reaches onError instead of looking like missing state. Custom stores should do the same: return null only when nothing is stored.
onDiscard
Section titled “onDiscard”Called once when saved state is rejected: expired, version_unmigrated, migration_rejected, or validation_rejected. Missing state and successful restores do not notify. Only expiration removes stored state.
export type FlowRestoreDiscardReason = | "expired" | "version_unmigrated" | "migration_rejected" | "validation_rejected" | "incompatible_state";
export interface FlowRestoreDiscardEvent { flowId: string; reason: FlowRestoreDiscardReason; fromVersion?: string; toVersion?: string; instanceId?: string; variantId?: string; errors?: string[];}Both types are exported by @useflow/core and @useflow/react. fromVersion is the saved __meta.version; toVersion is the restore option’s version when known. Only React emits incompatible_state and its errors array.
const persister = createPersister({ store, onDiscard: (event) => { Sentry.captureMessage("Flow progress discarded", { level: "warning", extra: { ...event }, }); },});The restore() option’s onDiscard overrides this default; they never both fire. React passes its resolved Flow/provider callback through this option. React’s incompatible_state check notifies only the Flow/provider onDiscard; when neither is set, it calls onPersistenceError instead.
Throwing callbacks do not interrupt restore. Failures go to onError, or a development-only warning if no handler exists. Flow/provider callback failures use onPersistenceError instead. See reporting discarded progress.
Custom storage backend
Section titled “Custom storage backend”Create a custom store for any backend:
API backend
Section titled “API backend”const apiStore: FlowStore = { get: async (flowId, options) => { const params = new URLSearchParams({ flowId, ...(options?.instanceId && { instanceId: options.instanceId }), ...(options?.variantId && { variantId: options.variantId }) }); const response = await fetch(`/api/flow-state?${params}`); if (!response.ok) return null; return response.json(); },
set: async (flowId, state, options) => { await fetch('/api/flow-state', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ flowId, state, ...options }) }); },
remove: async (flowId, options) => { const params = new URLSearchParams({ flowId, ...(options?.instanceId && { instanceId: options.instanceId }), ...(options?.variantId && { variantId: options.variantId }) }); await fetch(`/api/flow-state?${params}`, { method: 'DELETE' }); }};
const persister = createPersister({ store: apiStore });IndexedDB
Section titled “IndexedDB”const indexedDBStore: FlowStore = { get: async (flowId, options) => { const db = await openDB(); const key = formatKey(flowId, options); return db.get('flows', key); },
set: async (flowId, state, options) => { const db = await openDB(); const key = formatKey(flowId, options); await db.put('flows', state, key); },
remove: async (flowId, options) => { const db = await openDB(); const key = formatKey(flowId, options); await db.delete('flows', key); }};
// Helper to format keys consistentlyfunction formatKey(flowId: string, options?: FlowStoreOptions) { const parts = [flowId]; if (options?.variantId) parts.push(options.variantId); if (options?.instanceId) parts.push(options.instanceId); return parts.join(':');}
const persister = createPersister({ store: indexedDBStore });Persister methods
Section titled “Persister methods”The returned persister object has these methods:
save()
Section titled “save()”await persister.save( flowId: string, state: FlowState, options?: { version?: string; instanceId?: string; variantId?: string; onError?: (error: Error) => void; }): Promise<PersistedFlowState | null>restore()
Section titled “restore()”await persister.restore( flowId: string, options?: { version?: string; migrate?: MigrateFunction; instanceId?: string; variantId?: string; onDiscard?: (event: FlowRestoreDiscardEvent) => void; onError?: (error: Error) => void; }): Promise<PersistedFlowState | null>remove()
Section titled “remove()”await persister.remove( flowId: string, options?: { instanceId?: string; variantId?: string; onError?: (error: Error) => void; }): Promise<void>removeFlow()
Section titled “removeFlow()”await persister.removeFlow( flowId: string): Promise<void>Removes all instances of a specific flow (base + all instances).
removeAll()
Section titled “removeAll()”await persister.removeAll(): Promise<void>Removes all flows managed by this persister.
Storage keys
Section titled “Storage keys”The store implementation determines the key format. For key-value stores (localStorage, sessionStorage), keys typically follow this pattern:
[flowId]:[variantId]:[instanceId]Examples:
onboarding::- Default (no variant or instance)onboarding:v2:- With variantonboarding::user123- With instancecheckout:v2:session456- All options
Different stores may use different formats. Check your store’s documentation for specifics.
Best practices
Section titled “Best practices”Handle storage errors
Section titled “Handle storage errors”<Flow flow={myFlow} persister={persister} onPersistenceError={(error, { flowId, operation }) => { console.error(`Could not ${operation} ${flowId}:`, error); // Fallback or notify user }}> {/* ... */}</Flow>Clean up old data
Section titled “Clean up old data”// Remove specific flow stateawait persister.remove('onboarding', { instanceId: 'user123' });
// Remove all instances of a flowawait persister.removeFlow?.('onboarding');
// Clear all persisted dataawait persister.removeAll?.();Instance IDs for multiple flows
Section titled “Instance IDs for multiple flows”// Different persistence for each user<Flow flow={checkoutFlow} persister={persister} instanceId={userId} // Separate storage per user> {/* ... */}</Flow>Migration support
Section titled “Migration support”Handle breaking changes in your flow:
const migration: MigrateFunction = (oldState, fromVersion) => { if (fromVersion === "v1") { // Migrate v1 → v2 return { ...oldState, context: { ...oldState.context, newField: 'default' } }; } return oldState;};
// Flow will use migration function on restore<Flow flow={myFlow} persister={persister}> {/* ... */}</Flow>Related
Section titled “Related”- Persistence Guide - Complete persistence patterns
- Storage Stores - Built-in store details
- Custom Stores - Building custom backends