diff --git a/next-env.d.ts b/next-env.d.ts index 52e831b43..254b73c16 100644 --- a/next-env.d.ts +++ b/next-env.d.ts @@ -1,5 +1,6 @@ /// /// +/// // NOTE: This file should not be edited // see https://nextjs.org/docs/pages/api-reference/config/typescript for more information. diff --git a/src/components/Menu/MenuLinks.ts b/src/components/Menu/MenuLinks.ts index a625d5b83..128025265 100644 --- a/src/components/Menu/MenuLinks.ts +++ b/src/components/Menu/MenuLinks.ts @@ -145,6 +145,12 @@ export const apiLinks: Pages = [ { pathname: "/docs/usewatch", name: "useWatch", + pages: [ + { + pathname: "/docs/usewatch/watch", + name: "Watch", + }, + ], }, { pathname: "/docs/useformstate", diff --git a/src/content/docs/usewatch/watch.mdx b/src/content/docs/usewatch/watch.mdx new file mode 100644 index 000000000..b50f6a693 --- /dev/null +++ b/src/content/docs/usewatch/watch.mdx @@ -0,0 +1,50 @@ +--- +title: Watch +description: Watch component for subscribing to input changes +sidebar: apiLinks +--- + +## \ `Watch:` Component + +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 `` directly in your JSX to subscribe to and render form values. + +### Props + +--- + +| Name | Type | Description | +| -------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `name` | string \| string[] \| undefined | Name of the field. | +| `control` | Object | [`control`](/docs/useform/control) object provided by `useForm`. It's optional if you are using `FormProvider`. | +| `compute` | function |

Subscribe to selective and computed form values.

  • Subscribe to the entire form but only return updated value with certain condition { \n if (data.test?.length) return data.test; \n\n return ''; \n }, \n});`}/>
  • Subscribe to a specific form value state { \n return data.length ? data : ''; \n }, \n});`}/>
| +| `defaultValue` | unknown | default value for `useWatch` to return before the initial render.

**Note:** the first render will always return `defaultValue` when it's supplied. | +| `disabled` | boolean = false | Option to disable the subscription. | +| `exact` | boolean = false | This prop will enable an exact match for input name subscriptions. | +| `render` | Function | 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. | + +**Examples:** + +--- + +```tsx copy sandbox="" +import { useForm, Watch } from 'react-hook-form'; + +const App = () => { + const { register, control } = useForm(); + + return ( +
+
+ + +
+ {/* re-render only when value of `foo` changes */} + {foo}} + /> +
+ ); +}; +```