Persist
Keep a draft in storage so a reload doesn't lose it.
A reload, a crash or a closed tab loses what the user typed. persist()
keeps a draft in storage and puts it back.
Type something, then reload the page or press the demo’s reset button.
persist("feedback-draft") saves the form value as JSON in localStorage
under that key, once the page is idle after a write. Pass other storage as
the second argument, as in persist("feedback-draft", sessionStorage). Any
object with getItem, setItem and removeItem works.
Restoring
The saved value is written back when the store first gets a listener, as when
a component mounts a field. The restore is a plugin write, not a user edit, so
its fields aren’t marked edited and history makes no step from it.
The server never listens, so it never restores. The server render and the render that hydrates show the defaults, and the draft shows in the render after.
Clearing the draft
A reset writes the defaults, and persist saves those too, so the draft is
gone after the next reload. To stop saving, leave persist out of the list.
persist(key, undefined) still means localStorage.
When storage fails
Storage that throws, as when it is blocked or full, never breaks the form. A failed save is dropped and the next write tries again. A saved value that doesn’t parse is removed, and the form starts from its defaults.
Last updated