Form actions
Change form state from anywhere in the application
Docs
Package
Usage
Form actions allow changing state of the form from anywhere in your application. The mechanism of form actions is similar to notifications system, modals manager and other similar packages.
To use form actions, set name property in use-form settings:
Then call createFormActions function with the same form name as specified in useForm settings:
After that, you can use demoFormActions to change form state from anywhere in your application.
For example, after a fetch request or after a user interaction with a component that does not have access
to the form state:
Form name
Form name must be a string that contains only letters, numbers and dashes:
Note that form names must be unique, if you have multiple forms with the same name, form actions will update the state of all forms with the same name.
Form actions
createFormActions function returns an object with the following methods:
- setFieldValue
- setValues
- setInitialValues
- setErrors
- setFieldError
- clearFieldError
- clearErrors
- reset
- validate
- validateField
- reorderListItem
- removeListItem
- insertListItem
- setDirty
- setTouched
- resetDirty
- resetTouched
All methods work similar to use-form hooks methods – functions accept the same arguments but do not return anything.