Skip to content

Persistence

Restore drafts with a clear storage contract.

Persistence saves form values under a stable key, writes after a debounce, skips excluded fields, and can restore a draft on mount.

Configuration

Persistence is a useFormedible option with five properties. In the browser, Formedible uses the configured storage and falls back to sessionStorage unless you choose localStorage.

  • key is required and is passed to storage.setItem, getItem, and removeItem.
  • storage accepts localStorage or sessionStorage; blank means sessionStorage.
  • exclude removes top-level field names before Formedible writes the payload.
PropertyTypeDefaultDescription
key
string
RequiredStorage key for the saved draft. Include product area, form name, and a version, such as onboarding:v2.
storage
'localStorage' | 'sessionStorage'
'sessionStorage'Browser storage target. Use localStorage for drafts that should survive closed tabs.
debounceMs
number
500Delay after value changes before Formedible writes a draft.
exclude
readonly (Extract<keyof TFormValues, string> | string)[]
[]Field names removed from saved values before each write.
restoreOnMount
boolean
falseLoads the saved payload on mount and applies its values and page when present.

Auto-save behavior

The persistence hook watches form.state.values. In the browser, it schedules saveToStorage after debounceMs, or 500 ms by default, and cancels that write if values change first.

  • createPersistedFormPayload always writes values and timestamp.
  • currentPage is included only when Formedible passes a page number.
  • After onSubmit resolves, Formedible clears the configured storage key.

Manual controls

useFormedible returns the persistence helpers from useFormPersistence. Manual buttons and debounce writes use the same key, storage target, and payload format.

  • saveToStorage writes current form values with the current page from useFormedible.
  • loadFromStorage sets saved fields, restores a valid currentPage, and returns the parsed payload.
  • clearStorage removes the configured key when storage is available.

Payload structure

The stored value is JSON.stringify(payload). The parser returns undefined for bad JSON, missing values, or a missing timestamp.

  • values must parse as an object; timestamp must parse as a number.
  • currentPage is kept only when it parses as a number.
  • loadFromStorage calls form.setFieldValue for each saved field before returning the payload.
PropertyTypeDefaultDescription
values
Partial<TFormValues>
RequiredSaved field values after exclude runs.
timestamp
number
RequiredSave time in milliseconds since the Unix epoch.
currentPage
number
undefinedSaved page number for multi-page forms. It restores when the saved number fits within the current page count.

UX patterns

Formedible gives you storage mechanics, not a status banner. Build visible draft controls around the returned helpers and the timestamp from loadFromStorage.

  • Use restoreOnMount for automatic restore.
  • Call loadFromStorage from a button when users should choose whether to restore.
  • Change the key when field names change; the parser validates shape but does not migrate old drafts.

Formedible

TanStack Form, shadcn/ui, Zod