What You’ll Learn
In this tutorial, you’ll learn how to pre-fill FormEngine forms with default values using theinitialData prop. We’ll cover:
- Setting static default values
- Loading and pre-filling data from an API (edit forms)
- Pre-filling from URL parameters
- Saving and restoring form data with localStorage
- Partial pre-fill (only some fields)
- Common pitfalls and how to avoid them
Basic: Static Default Values
The simplest way to pre-fill a form is with theinitialData prop. Pass an object where keys match your JSON schema field keys:
initialData that exist in your JSON schema. Extra keys are ignored.
Edit Form: Load Data from API
A common use case is pre-filling a form when editing an existing record. Load the data from an API and pass it asinitialData:
Pre-fill from URL Parameters
Use React Router’suseSearchParams (or Next.js URL search params) to populate form fields from query parameters:
Pre-fill from localStorage (Save Draft)
Allow users to save their progress and restore it on the next visit:- Loads the draft from
localStoragewhen the component mounts (viauseStateinitializer function) - Saves the form data to
localStorageevery time the user makes a change - Clears the draft after successful submission
Partial Pre-fill (Only Some Fields)
You don’t need to provide values for every field. Include only the fields you want to pre-fill:initialData will render empty or with their placeholder text. This is useful when you only have partial data.
What Happens When initialData Changes After Mount?
Important: TheinitialData prop is applied only when the form first mounts. If you change initialData after the form is already rendered, the form will NOT automatically update to the new values.
viewerRef to programmatically set the data:
key:
Common Mistakes
1. Field Keys Don’t Match the Schema
If your JSON schema has a field with keyfirstName but you provide initialData={{ firstName: 'John' }}, it works. But if you use initialData={{ first_name: 'John' }}, the field won’t be pre-filled because the key doesn’t match.
2. Undefined vs Empty String
undefined and empty string '' are treated differently. Use empty strings for “no value,” not undefined:
3. Assuming initialData Persists After Changes
initialData is just the starting point. When the user edits a field, the form’s internal state updates, but initialData doesn’t change. Don’t rely on initialData to reflect the current form state—use onFormDataChange callback or viewerRef.current.formData instead.
4. Not Handling Async Data Load
If you’re loading initial data from an API, the form might render before the data arrives. Show a loading state or provide an emptyinitialData:
Next Steps
Now that you can pre-fill forms, explore these related topics:- Form Data Handling — Learn how to read and update form data programmatically
- Form Submission — Handle form submission with validation and API calls
- Reset Form — Clear a form after submission or restore it to initial state
- Tracking Form Changes — Monitor real-time form changes with
onFormDataChange