TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ludwiigdev/Heroes_App/llms.txt
Use this file to discover all available pages before exploring further.
useForm hook is a reusable, generic solution for managing controlled form state in React. Instead of writing useState and change-handler boilerplate for every form in the application, useForm accepts an initial field map and returns each field value directly alongside a unified onInputChange handler and an onResetForm utility — keeping form components concise and consistent across the app.
Source
Parameter
An object whose keys represent form field names and whose values are the corresponding initial values. Every key in this object becomes a field tracked by the hook. Pass an empty object
{} to start with no fields.Return Value
The hook spreads allinitialForm fields directly into the returned object, so each field is accessible by name without any nesting. It also exposes the raw state object and both handlers.
Each key from
initialForm is spread at the top level of the return value. For example, if you pass { searchText: '' }, the hook returns searchText directly — not formState.searchText. Destructure the field names you need as you would any other return value.The complete form state object. Use this when you need to read all fields at once — for example, when submitting a form or passing the full payload to another function.
An event handler to pass directly to any
<input>, <select>, or <textarea> element via its onChange prop. Internally it reads event.target.name and event.target.value and updates only the matching field, leaving all other fields untouched.Every input element wired to
onInputChange must have a name attribute whose value matches the corresponding key in initialForm. If the name is missing or mismatched, a new key will be created in formState rather than updating the intended field.Resets all form fields back to the values originally passed in
initialForm. Useful for “Cancel” buttons, form submission success handlers, or any scenario where the form should return to its default state.How onInputChange Works
When an input fires a change event, onInputChange destructures name and value from event.target, then calls setFormState with a shallow merge that overwrites the matching key:
onChange wrappers needed.
Usage Example
The example below shows a two-field registration form withname and email. Notice how the name attribute on each <input> matches the key passed to useForm.
Real-World Example: SearchPage
Heroes App’sSearchPage uses useForm with a single searchText field that is pre-populated from the URL query parameter q. This lets the search input stay in sync with the browser’s address bar on page load:
Naming Tip
BecauseuseForm spreads field keys at the top level of its return value, you can destructure them by name just like any other variable:
formState only when you need to pass the entire field set to another function (such as a form submission handler) — for individual field reads, rely on the spread values.