Skip to content

Commit eda888d

Browse files
committed
feat: update doc on Watch component
1 parent 98133e6 commit eda888d

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

next-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3+
/// <reference path="./.next/types/routes.d.ts" />
34

45
// NOTE: This file should not be edited
56
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.

src/components/Menu/MenuLinks.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ export const apiLinks: Pages = [
145145
{
146146
pathname: "/docs/usewatch",
147147
name: "useWatch",
148+
pages: [
149+
{
150+
pathname: "/docs/usewatch/watch",
151+
name: "Watch",
152+
},
153+
],
148154
},
149155
{
150156
pathname: "/docs/useformstate",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: Watch
3+
description: Watch component for subscribing to input changes
4+
sidebar: apiLinks
5+
---
6+
7+
## \</> `Watch:` <TypeText>Component</TypeText>
8+
9+
A React Hook Form component that provides the same functionality as `useWatch`, but in component form. Instead of using the hook inside another component, you can use `<Watch />` directly in your JSX to subscribe to and render form values.
10+
11+
### Props
12+
13+
---
14+
15+
| Name | Type | Description |
16+
| -------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
17+
| `name` | <TypeText>string \| string[] \| undefined</TypeText> | Name of the field. |
18+
| `control` | <TypeText>Object</TypeText> | [`control`](/docs/useform/control) object provided by `useForm`. It's optional if you are using `FormProvider`. |
19+
| `compute` | <TypeText>function</TypeText> | <p>Subscribe to selective and computed form values.</p><ul><li>Subscribe to the entire form but only return updated value with certain condition<CodeArea withOutCopy rawData={`const watchedValue = useWatch({\n compute: (data: FormValue) => { \n if (data.test?.length) return data.test; \n\n return ''; \n }, \n});`}/></li><li>Subscribe to a specific form value state<CodeArea withOutCopy rawData={`const watchedValue = useWatch({\n name: 'test', \n compute: (data: string) => { \n return data.length ? data : ''; \n }, \n});`}/></li></ul> |
20+
| `defaultValue` | <TypeText>unknown</TypeText> | default value for `useWatch` to return before the initial render.<br/><br/>**Note:** the first render will always return `defaultValue` when it's supplied. |
21+
| `disabled` | <TypeText>boolean = false</TypeText> | Option to disable the subscription. |
22+
| `exact` | <TypeText>boolean = false</TypeText> | This prop will enable an exact match for input name subscriptions. |
23+
| `render` | <TypeText>Function</TypeText> | Subscribes to specified form field(s) and re-renders its child function whenever the values change. This allows you to declaratively consume form values in JSX without manually wiring up state. |
24+
25+
**Examples:**
26+
27+
---
28+
29+
```tsx copy sandbox=""
30+
import { useForm, Watch } from 'react-hook-form';
31+
32+
const App = () => {
33+
const { register, control } = useForm();
34+
35+
return (
36+
<div>
37+
<form>
38+
<input {...register('foo')} />
39+
<input {...register('bar')} />
40+
</form>
41+
{/* re-render only when value of `foo` changes */}
42+
<Watch
43+
control={control}
44+
names={['foo']}
45+
render={([foo]) => <span>{foo}</span>}
46+
/>
47+
</div>
48+
);
49+
};
50+
```

0 commit comments

Comments
 (0)