Skip to content

Commit 4bf6a5b

Browse files
authored
fix: Use useWatch hook instead of form.watch for performance (#531)
1 parent ba35277 commit 4bf6a5b

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

src/components/Form/FieldSelect/docs.stories.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Box, Button, Stack } from '@chakra-ui/react';
22
import { zodResolver } from '@hookform/resolvers/zod';
3-
import { useForm } from 'react-hook-form';
3+
import { useForm, useWatch } from 'react-hook-form';
44
import { z } from 'zod';
55

66
import { Form, FormField, FormFieldController, FormFieldLabel } from '../';
@@ -112,6 +112,7 @@ export const Disabled = () => {
112112

113113
export const ChakraProps = () => {
114114
const form = useForm<FormSchema>(formOptions);
115+
const color = useWatch({ name: 'color', control: form.control });
115116

116117
return (
117118
<Form {...form} onSubmit={(values) => console.log(values)}>
@@ -128,7 +129,7 @@ export const ChakraProps = () => {
128129
chakraStyles: {
129130
control: (provided) => ({
130131
...provided,
131-
backgroundColor: `${form.watch('color')}.100`,
132+
backgroundColor: `${color}.100`,
132133
}),
133134
},
134135
}}

src/features/account/AccountEmailForm.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react';
33
import { Button, Flex, Stack } from '@chakra-ui/react';
44
import { zodResolver } from '@hookform/resolvers/zod';
55
import { parseAsString, useQueryStates } from 'nuqs';
6-
import { SubmitHandler, useForm } from 'react-hook-form';
6+
import { SubmitHandler, useForm, useWatch } from 'react-hook-form';
77
import { useTranslation } from 'react-i18next';
88

99
import { ErrorPage } from '@/components/ErrorPage';
@@ -57,7 +57,7 @@ export const AccountEmailForm = () => {
5757
},
5858
});
5959

60-
const email = form.watch('email');
60+
const email = useWatch({ name: 'email', control: form.control });
6161

6262
const onSubmit: SubmitHandler<FormFieldsAccountEmail> = (values) => {
6363
updateEmail.mutate(values);

src/features/auth/PageRegister.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Box, Button, Flex, Heading, Stack } from '@chakra-ui/react';
44
import { zodResolver } from '@hookform/resolvers/zod';
55
import Link from 'next/link';
66
import { useRouter } from 'next/navigation';
7-
import { useForm } from 'react-hook-form';
7+
import { useForm, useWatch } from 'react-hook-form';
88
import { useTranslation } from 'react-i18next';
99

1010
import {
@@ -54,7 +54,7 @@ export default function PageRegister() {
5454
},
5555
});
5656

57-
const language = form.watch('language');
57+
const language = useWatch({ name: 'language', control: form.control });
5858

5959
// Change language based on form
6060
useEffect(() => {

0 commit comments

Comments
 (0)