Skip to content

Update md file by adding a simple example #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/api/useField.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,41 @@ An object that looks just like [`FieldProps`](../types/FieldProps), except witho
`useField()` returns [`FieldRenderProps`](../types/FieldRenderProps). It will manage the rerendering of any component you use it in, i.e. the component will only rerender if the field state subscribed to via `useField()` changes.

`useField()` is used internally inside [`<Field/>`](Field).


## Example

```ts
import { useForm, useField } from 'react-final-form-hooks'

const MyForm = () => {
const { form, handleSubmit, values, pristine, submitting } = useForm({
onSubmit, // the function to call with your form values upon valid submit
validate // a record-level validation function to check all form values
})
const firstName = useField('firstName', form)
const lastName = useField('lastName', form)
return (
<form onSubmit={handleSubmit}>
<div>
<label>First Name</label>
<input {...firstName.input} placeholder="First Name" />
{firstName.meta.touched && firstName.meta.error && (
<span>{firstName.meta.error}</span>
)}
</div>
<div>
<label>Last Name</label>
<input {...lastName.input} placeholder="Last Name" />
{lastName.meta.touched && lastName.meta.error && (
<span>{lastName.meta.error}</span>
)}
</div>
<button type="submit" disabled={pristine || submitting}>
Submit
</button>
</form>
)
}

```