Skip to content

Commit 37f5981

Browse files
committed
feat(FormStateSubscribe): add docs for FormStateSubscribe
1 parent a8fad1a commit 37f5981

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/components/Menu/MenuLinks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ export const apiLinks: Pages = [
160160
pathname: "/docs/useformstate/errormessage",
161161
name: "ErrorMessage",
162162
},
163+
{
164+
pathname: "/docs/useformstate/formstatesubscribe",
165+
name: "FormStateSubscribe",
166+
},
163167
],
164168
},
165169
{

src/content/docs/useformstate.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ sidebar: apiLinks
77
<SelectNav
88
options={[
99
{
10-
label: "ErrorMessage",
11-
value: "/docs/errormessage",
10+
label: "FormStateSubscribe",
11+
value: "/docs/formstatesubscribe",
1212
},
1313
]}
1414
/>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: FormStateSubscribe
3+
description: Component to subscribe to form state update
4+
sidebar: apiLinks
5+
---
6+
7+
## \</> `FormStateSubscribe:` <TypeText>Component</TypeText>
8+
9+
A React Hook Form component that provides the same functionality as `uesFormState`, but in component form. Instead of using the hook inside another component, you can use `<FormStateSubscribe />` directly in your JSX to subscribe to and render form state.
10+
11+
### Props
12+
13+
---
14+
15+
| Name | Type | Description |
16+
| ---------- | ---------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
17+
| `control` | <TypeText>Object</TypeText> | [`control`](/docs/useform/control) object provided by `useForm`. It's optional if you are using `FormProvider`. |
18+
| `name` | <TypeText>string \| string[] </TypeText> | Provide a single input name, an array of them, or subscribe to all inputs' formState update. |
19+
| `disabled` | <TypeText>boolean = false</TypeText> | Option to disable the subscription. |
20+
| `exact` | <TypeText>boolean = false</TypeText> | This prop will enable an exact match for input name subscriptions. |
21+
| `render` | <TypeText>Function</TypeText> | Subscribes to form state of specified form field(s) and re-renders its child function whenever the form state changes. This allows you to declaratively consume form state in JSX without manually wiring up state. |
22+
23+
**Examples:**
24+
25+
---
26+
27+
```tsx copy sandbox=""
28+
import { useForm, FormStateSubscribe } from "react-hook-form"
29+
30+
const App = () => {
31+
const { register, control } = useForm()
32+
33+
return (
34+
<div>
35+
<form>
36+
<input {...register("foo", { min: 3 })} />
37+
<input {...register("bar")} />
38+
{/* re-render only when form state of `foo` changes */}
39+
<FormStateSubscribe
40+
control={control}
41+
name="foo"
42+
render={({ errors }) => <span>{errors.foo?.message}</span>}
43+
/>
44+
</form>
45+
</div>
46+
)
47+
}
48+
```
49+
50+
```

0 commit comments

Comments
 (0)