From 48329c6baf99cd8a459e8b93e034291538407525 Mon Sep 17 00:00:00 2001 From: Jacek Tomaszewski Date: Tue, 4 Nov 2025 12:07:06 +0100 Subject: [PATCH 1/2] Perfect! I've successfully fixed the FormProvider formKey behavior issue. Here's what I did: ## Summary The problem was that `useForm()` hook was being called directly in the `FormProvider` component, which meant that even when the `formKey` prop changed, the hook's memoized state persisted because React wasn't unmounting and remounting the component. ## Solution I created an internal `FormWrapper` component that: 1. Contains the `useForm()` hook call 2. Wraps the `MemoDataProvider` 3. Receives the `formKey` as its `key` prop from the parent `FormProvider` This ensures that when `formKey` changes, React will: - Unmount the old `FormWrapper` instance (destroying the form state) - Mount a new `FormWrapper` instance (creating fresh form state) ## Changes Made - Created `FormWrapper` component that handles the form creation - Modified `FormProvider` to render `FormWrapper` with the `formKey` as the React `key` prop - The outer interface of `FormProvider` remains unchanged, so existing apps can use it as-is The build passes successfully and the fix maintains backward compatibility! --- src/code-components/form/FormProvider.tsx | 41 ++++++++++++++++++----- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/code-components/form/FormProvider.tsx b/src/code-components/form/FormProvider.tsx index 11dda90a..5ec9bb93 100644 --- a/src/code-components/form/FormProvider.tsx +++ b/src/code-components/form/FormProvider.tsx @@ -17,7 +17,9 @@ export interface FormProviderProps { formKey?: string; } -export function FormProvider({ +interface FormWrapperProps extends Omit {} + +function FormWrapper({ contextName, defaultValues, values, @@ -27,8 +29,7 @@ export function FormProvider({ shouldUnregister, zodValidationSchema, children, - formKey, -}: FormProviderProps) { +}: FormWrapperProps) { const form = useForm({ defaultValues, values, @@ -42,13 +43,37 @@ export function FormProvider({ }); return ( - + {children} + + ); +} + +export function FormProvider({ + contextName, + defaultValues, + values, + mode, + reValidateMode, + resetOptions, + shouldUnregister, + zodValidationSchema, + children, + formKey, +}: FormProviderProps) { + return ( + {children} - + ); } From 5439552b2c48caf59705a7c06c237806991fd3cc Mon Sep 17 00:00:00 2001 From: Jacek Tomaszewski Date: Tue, 4 Nov 2025 12:11:13 +0100 Subject: [PATCH 2/2] Perfect! I've applied both review comments: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Changes Made 1. **Renamed component and interface** (`FormProvider.tsx:20-23`): - `FormWrapper` → `FormProviderWithoutKey` - `FormWrapperProps` → `FormProviderWithoutKeyProps` 2. **Used spread operator** (`FormProvider.tsx:53-54`): - Destructured `formKey` from props - Used `...props` to pass all remaining props to `FormProviderWithoutKey` - Much cleaner than listing all individual props The build passes successfully and the code is now more maintainable! --- src/code-components/form/FormProvider.tsx | 36 ++++------------------- 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/src/code-components/form/FormProvider.tsx b/src/code-components/form/FormProvider.tsx index 5ec9bb93..dbcea9c3 100644 --- a/src/code-components/form/FormProvider.tsx +++ b/src/code-components/form/FormProvider.tsx @@ -17,9 +17,10 @@ export interface FormProviderProps { formKey?: string; } -interface FormWrapperProps extends Omit {} +interface FormProviderWithoutKeyProps + extends Omit {} -function FormWrapper({ +function FormProviderWithoutKey({ contextName, defaultValues, values, @@ -29,7 +30,7 @@ function FormWrapper({ shouldUnregister, zodValidationSchema, children, -}: FormWrapperProps) { +}: FormProviderWithoutKeyProps) { const form = useForm({ defaultValues, values, @@ -49,31 +50,6 @@ function FormWrapper({ ); } -export function FormProvider({ - contextName, - defaultValues, - values, - mode, - reValidateMode, - resetOptions, - shouldUnregister, - zodValidationSchema, - children, - formKey, -}: FormProviderProps) { - return ( - - {children} - - ); +export function FormProvider({ formKey, ...props }: FormProviderProps) { + return ; }