Skip to content

Commit 3ce1ab3

Browse files
authored
fix: Optimize performance of the form field error (#530)
1 parent debe6f2 commit 3ce1ab3

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/components/Form/FormFieldError.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { Flex, FlexProps, SlideFade } from '@chakra-ui/react';
44
import {
55
ControllerProps,
66
FieldError,
7+
FieldErrors,
78
FieldPath,
89
FieldValues,
10+
get,
911
useFormContext,
1012
} from 'react-hook-form';
1113
import { LuAlertCircle } from 'react-icons/lu';
@@ -39,7 +41,9 @@ const FormFieldErrorComponent = <
3941
TFieldValues,
4042
TName
4143
> | null>(FormFieldControllerContext as ExplicitAny);
42-
const { formState } = useFormContext<TFieldValues, TName>();
44+
const {
45+
formState: { errors },
46+
} = useFormContext<TFieldValues, TName>();
4347
const control = 'control' in props ? props.control : ctx?.control;
4448
const name = 'name' in props ? props.name : ctx?.name;
4549

@@ -49,7 +53,7 @@ const FormFieldErrorComponent = <
4953
);
5054
}
5155

52-
const { error } = control.getFieldState(name, formState);
56+
const error = get<FieldErrors<TFieldValues>>(errors, name);
5357

5458
if (!error) {
5559
return null;

0 commit comments

Comments
 (0)