Easily create forms in React components -- with hooks! Essentially a dumbed down version of Formik using hooks.
npm install form-hooks
import { useForm } from 'form-hooks';
const Sample = () => (
const {
errors,
touched,
values,
handleBlur,
handleChange,
handleSubmit,
isSubmitting,
} = useForm({
initialValues: {
name: '',
email: '',
},
onSubmit: values => fetch(/* with values */),
validate: values => ({
...(!values.name.length ? { name: 'Requires a name' } : {}),
...(!values.email.length ? { email: 'Requires an email' } : {})
}),
}, options => [options.initialValues]);
return (
<form onSubmit={handleSubmit}>
<input name="name"
type="text"
value={values.name}
onChange={handleChange}
onBlur={handleBlur}
/>
{touched['name'] && errors['name']}
<input name="email"
type="text"
value={values.email}
onChange={handleChange}
onBlur={handleBlur}
/>
{touched['email'] && errors['email']}
<button type="submit" disabled={isSubmitting}>submit</button>
</form>
)
)
useForm<Values>(options: FormHookOptions, dependencies?: FormHookDependencies<Values>): FormHookState<Values>
- FormHookOptions
The useForm
hook takes some options (as an object) to initialize state
and manage form validation/submissions.
An object with the forms initial values. These values should not be required.
Called when a form is submitted with the values set. Only called if validation passes.
Called when a form is submitted prior to the onSubmit
call. Returns an object
of errors similar to Formik
.
Indicates if useForm
should re-validate the input on blur.
Only fired when all fields have been touched that were in the initialValues
object.
Indicates if useForm
should re-validate the input on change.
Only fired when all fields have been touched that were in the initialValues
object.
useForm<Values>(options: FormHookOptions, dependencies?: FormHookDependencies<Values>): FormHookState<Values>
- FormHookState
An object that contains the form errors where the key is the field name and the value is the error message.
An object that contains which form fields have been touched. The key is the field name, the value is a boolean of if the field has been touched.
An object that contains all of the values of the form. Initialized with the
initialValues
originally passed in. Modified by the handleChange
handler.
Marks a field as touched
to show errors after all fields are touched.
Changes the fields value in the values
state.
Handles calling validation prior to the onSubmit
handler and setting the
touched
, errors
and isSubmitting
state internally.
Boolean value if the form is currently submitting.
Number of times the form was submitted.
Function that allows for errors to be set outside of the useForm
internal handlers (good for handling request errors).
Sets a field to touched or untouched. Triggers a re-validation if
validateOnBlur
is enabled and all fields have been touched.
Sets a fields value. Re-valdiates if validateOnChange
is enabled and
all required conditions are met.
Resets a form to its initial state.
Resets a field value to its initial value and resets its errors state. Can optionally reset its touched state.
The second parameter to useForm
allows for a list of dependencies to be
declared from the collection of options passed through in the first argument.
A change in a dependency will reset the form. For instance in this example:
useForm(
{
initialValues: { first: 'John', last: 'Doe' },
onSubmit: () => {},
validate: () => ({}),
},
options => [options.initialValues]
);
Changing the initialValues
object will cause the Form to be re-initialized. initialValues
, errors
, touched
, submitCount
and isSubmitting
will be reset.