Update README.md

This commit is contained in:
Nicco 2019-10-19 16:56:43 +02:00 committed by GitHub
parent 4650339251
commit ff1fe80bbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,7 +39,9 @@ npm i formhero
- [form](#form)
- [errors](#errors)
- [isValid](#isvalid)
- [update](#update)
- [setField](#setfield)
- [setForm](#setform)
- [setErrors](#seterrors)
## 🤔 Motivation
@ -360,27 +362,48 @@ const { errors } = useForm(...)
`isValid` is a little simple helper that checks whether the `error` object is clear or if there are errors left.
## Update
## setField
The `update` function allows you to manually change and assign the state of the form. This can be usefull when you want to reset a field or the whole form. The input must have the same type as the initial state.
The `setField` function allows you to manually change and assign the state of a field. The type of the field must be the same as the initial type given in the constructor.
###### Example
```javascript
const { form, update } = useForm(...)
const { form, setField } = useForm(...)
const resetUsername = () => {
update({
...form,
username: '',
})
setField('username', 'new value')
}
```
## setForm
The `setForm` function allows you to manually change and assign the state of the form. This can be usefull when you want to reset a field or the whole form. The input must have the same type as the initial state.
###### Example
```javascript
const initial = {username: '', password: ''}
const { form, setForm } = useForm(initial, ...)
const resetForm = () => {
update({
username: '',
password: '',
})
setForm(initial)
}
```
## setErrors
The `setErrors` function allows you to manually change and assign the state of the errors. This can be usefull when you want to set an error manually (e.g. sent from the server).
###### Example
```javascript
const { form, setErrors } = useForm(...)
const setError = () => {
setErrors({username: 'Already taken'})
}
```