Skip to content

feat: useWatch compute prop #1145

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

Merged
merged 4 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/components/sponsorsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function SponsorsList() {
target="_blank"
rel="noopener noreferrer"
>
<img src="/images/route4me.png" alt="route4me"/>
<img src="/images/route4me.png" alt="route4me" />
</a>
<a
href="https://www.follower24.de/"
Expand Down
15 changes: 8 additions & 7 deletions src/content/docs/usewatch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ Behaves similarly to the `watch` API, however, this will isolate re-rendering at

---

| Name | Type | Description |
| -------------- | ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name` | <TypeText>string \| string[] \| undefined</TypeText> | Name of the field. |
| `control` | <TypeText>Object</TypeText> | [`control`](/docs/useform/control) object provided by `useForm`. It's optional if you are using `FormProvider`. |
| `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. |
| `disabled` | <TypeText>boolean = false</TypeText> | Option to disable the subscription. |
| `exact` | <TypeText>boolean = false</TypeText> | This prop will enable an exact match for input name subscriptions. |
| Name | Type | Description |
| -------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `name` | <TypeText>string \| string[] \| undefined</TypeText> | Name of the field. |
| `control` | <TypeText>Object</TypeText> | [`control`](/docs/useform/control) object provided by `useForm`. It's optional if you are using `FormProvider`. |
| `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> |
| `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. |
| `disabled` | <TypeText>boolean = false</TypeText> | Option to disable the subscription. |
| `exact` | <TypeText>boolean = false</TypeText> | This prop will enable an exact match for input name subscriptions. |

### Return

Expand Down
7 changes: 4 additions & 3 deletions src/content/ts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ type FormValues = {
export default function App() {
const { register, handleSubmit } = useForm<FormValues>()
const onSubmit: SubmitHandler<FormValues> = (data) => console.log(data)
const onError: SubmitErrorHandler<FormValues> = (errors) => console.log(errors);
const onError: SubmitErrorHandler<FormValues> = (errors) =>
console.log(errors)

return (
<form onSubmit={handleSubmit(onSubmit, onError)}>
<input {...register("firstName"), { required: true }} />
<input {...register("lastName"), { minLength: 2 }} />
<input {...(register("firstName"), { required: true })} />
<input {...(register("lastName"), { minLength: 2 })} />
<input type="email" {...register("email")} />

<input type="submit" />
Expand Down