Automatically save form data to browser storage and restore it when users return. Perfect for long forms, multi-step wizards, and improving user experience.
Enable form persistence with a simple configuration to automatically save user data.
Enable form persistence by adding a persistence
configuration to your form. The form data will be automatically saved as users type and restored when they return.
const { Form } = useFormedible({fields: [{ name: 'firstName', type: 'text', label: 'First Name' },{ name: 'lastName', type: 'text', label: 'Last Name' },{ name: 'email', type: 'email', label: 'Email' },],persistence: {key: 'user-registration-form',storage: 'localStorage',restoreOnMount: true}});
Customize persistence behavior with storage type, debouncing, and field exclusion.
persistence: {key: 'my-form-data', // Unique storage keystorage: 'localStorage', // 'localStorage' or 'sessionStorage'debounceMs: 1000, // Debounce save operations (default: 1000ms)exclude: ['password', 'ssn'], // Fields to exclude from persistencerestoreOnMount: true // Restore data when component mounts}
Data persists across browser sessions until manually cleared. Best for forms users might return to later.
Data persists only for the current browser session. Best for temporary data or sensitive forms.
Prevent sensitive information from being saved to browser storage for security.
Use the exclude
option to prevent sensitive fields from being saved to storage:
const { Form } = useFormedible({fields: [{ name: 'username', type: 'text', label: 'Username' },{ name: 'password', type: 'password', label: 'Password' },{ name: 'creditCard', type: 'text', label: 'Credit Card' },{ name: 'email', type: 'email', label: 'Email' },],persistence: {key: 'registration-form',storage: 'localStorage',exclude: ['password', 'creditCard'], // These won't be savedrestoreOnMount: true}});
Access storage functions directly for manual control over save, load, and clear operations.
Access storage functions directly for manual control over when data is saved or cleared:
const {Form,saveToStorage,loadFromStorage,clearStorage} = useFormedible({// ... configuration});// Manual operationsconst handleSave = () => {const currentData = form.state.values;saveToStorage(currentData);};const handleLoad = () => {const savedData = loadFromStorage();if (savedData) {// Handle loaded dataconsole.log('Loaded data:', savedData);}};const handleClear = () => {clearStorage();};
Persistence automatically works with multi-step forms, saving both data and current page.
Persistence automatically works with multi-page forms, saving both form data and current page:
const { Form } = useFormedible({fields: [{ name: 'firstName', type: 'text', label: 'First Name', page: 1 },{ name: 'lastName', type: 'text', label: 'Last Name', page: 1 },{ name: 'email', type: 'email', label: 'Email', page: 2 },{ name: 'phone', type: 'tel', label: 'Phone', page: 2 },],persistence: {key: 'multi-step-form',storage: 'localStorage',restoreOnMount: true}});// When user returns, they'll be on the same page with their data restored
Form data is automatically cleared from storage after successful form submission.
Form data is automatically cleared from storage when the form is successfully submitted:
const { Form } = useFormedible({fields: [// ... your fields],persistence: {key: 'contact-form',storage: 'localStorage',restoreOnMount: true},formOptions: {onSubmit: async ({ value }) => {// Submit form dataawait submitToAPI(value);// Storage is automatically cleared after successful submission// No manual cleanup needed!}}});
Graceful error handling ensures forms work even when storage operations fail.
Persistence operations are wrapped in try-catch blocks and will log warnings if storage fails:
// Storage operations handle errors gracefully// If localStorage is full or disabled, the form continues to work normally// Check browser console for persistence warnings// You can also handle storage errors in your application:const { Form, saveToStorage } = useFormedible({// ... configuration});const handleManualSave = () => {try {saveToStorage(form.state.values);console.log('Data saved successfully');} catch (error) {console.warn('Failed to save form data:', error);// Handle error (show user notification, etc.)}};
Guidelines for effective persistence implementation and optimal user experience.
Use descriptive, unique keys for each form to avoid conflicts between different forms.
Always exclude passwords, credit cards, and other sensitive information from persistence.
Use localStorage for forms users might return to later, sessionStorage for temporary data.
Adjust debounce timing based on form complexity - longer for complex forms to reduce storage writes.
Start building forms with automatic data persistence and recovery. Create resilient user experiences that never lose user progress.