From 4dfec0f18f9dfe69c4576e57fcb1a9daf974f055 Mon Sep 17 00:00:00 2001 From: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> Date: Sun, 23 Jun 2024 00:20:32 +1200 Subject: [PATCH] refactor: use the `useWatch` from react-hook-form to get around the react-compiler memoization issues the react-compiler doesn't play well with the `form.watch` api as it breaks the rules-of-react in an unobvious way, thus loosing the reactivity that was intended. to get around this you need to wrap it in `useMemo` to trick the compilter. this change consumes the `form.control` in the provided `useWatch` hook by `react-hook-form` to maintain the reactivity of the original api without breaking the rules-of-react, thus letting the react-compilter work flawlessly without any hacks. --- .../customer-information/customer-stage.tsx | 9 +---- .../rates-and-charges/rates-stage.tsx | 40 +++++++------------ .../rental-information/duration-stage.tsx | 27 ++++++------- .../rental-information/vehicle-stage.tsx | 19 +++------ .../widget-grid/widgets/quick-lookup.tsx | 9 +---- .../application/location-edit-dialog.tsx | 9 +---- .../application/role-edit-dialog.tsx | 9 +---- .../_auth/(settings)/settings.profile.tsx | 9 +---- 8 files changed, 44 insertions(+), 87 deletions(-) diff --git a/src/components/add-rental/customer-information/customer-stage.tsx b/src/components/add-rental/customer-information/customer-stage.tsx index b4eb2bfb..f9a148f3 100644 --- a/src/components/add-rental/customer-information/customer-stage.tsx +++ b/src/components/add-rental/customer-information/customer-stage.tsx @@ -1,6 +1,6 @@ import * as React from "react"; import { zodResolver } from "@hookform/resolvers/zod"; -import { useForm } from "react-hook-form"; +import { useForm, useWatch } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { z } from "zod"; @@ -108,12 +108,7 @@ export const CustomerStage = ({ values: customerInformation ? values : undefined, }); - const form_dob = React.useMemo( - () => form.watch("dateOfBirth"), - // eslint-disable-next-line react-compiler/react-compiler - // eslint-disable-next-line react-hooks/exhaustive-deps - [form.watch("dateOfBirth")] - ); + const form_dob = useWatch({ control: form.control, name: "dateOfBirth" }); return (