Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b33b30

Browse files
WIP πŸ“ docs(handleSubmit): add desc skeleton
1 parent 319fd53 commit 1b33b30

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

β€Žsrc/markdown/handleSubmit.mdβ€Ž

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# `handleSubmit`
2+
3+
`handleSubmit` is a `function` that validates form values and triggers form submission.
4+
5+
```js
6+
handleSubmit(onValid, onInvalid)
7+
```
8+
9+
## Reference
10+
11+
### `handleSubmit(onValid, onInvalid)`
12+
13+
`handleSubmit` execution will trigger a chain of form submission events.
14+
15+
```jsx
16+
import { useForm } from 'react-hook-form'
17+
18+
function Form () {
19+
const { handleSubmit } = useForm()
20+
21+
const onSubmit = (data, event) => {
22+
// ...
23+
}
24+
25+
const onError = (errors, event) => {
26+
// ...
27+
}
28+
29+
return (
30+
<form onSubmit={handleSubmit(onSubmit, onError)}>
31+
{/* ... */}
32+
</form>
33+
)
34+
}
35+
```
36+
37+
<!-- Mention formState updates during and after the validation process -->
38+
39+
> **Note**
40+
> `handleSubmit` can be called on events other than `submit`. For example, you can call it on `onClick` or `onTouchEnd` events.
41+
> ```jsx
42+
> return <button onClick={handleSubmit(onSubmit, onError)}>Submit</button>
43+
> ```
44+
45+
### Options
46+
47+
#### `onValid`
48+
49+
[`onValid`](#onvalid) is a callback `function` that will be invoked when the form is submitted and there are no validation [`errors`](errors). It receives the form values as the first argument and the propagated `event` as the second argument.
50+
51+
```jsx
52+
const onSubmit = (data, event) => {
53+
// ...
54+
}
55+
```
56+
57+
`onValid` can also be an `async` function.
58+
59+
```jsx
60+
const onSubmit = async (data, event) => {
61+
await fetch('/api/user', {
62+
method: 'POST',
63+
body: JSON.stringify(data)
64+
})
65+
}
66+
```
67+
68+
#### `onInvalid`
69+
70+
[`onInvalid`](#oninvalid) is a callback `function` that will be invoked when the form is submitted and there are validation errors. It receives the form [`errors`](errors) as the first argument and the propagated `event` as the second argument.
71+
72+
```jsx
73+
const onError = (errors, event) => {
74+
// ...
75+
}
76+
```

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /