Skip to content

Commit 4d1d21a

Browse files
Fix useOnFormValueChange hook null handling and update stories
- Make useOnFormValueChange hook more defensive by checking for null formMethods - Add loading checks to story components to prevent null handleSubmit errors - Switch from fetcher.Form to regular form elements in stories - Hook now gracefully handles cases where form context is not available
1 parent 209e246 commit 4d1d21a

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

apps/docs/src/remix-hook-form/use-on-form-value-change.stories.tsx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ const CascadingDropdownExample = () => {
9696
},
9797
});
9898

99+
// Don't render if methods is not ready
100+
if (!methods.handleSubmit) {
101+
return <div>Loading...</div>;
102+
}
103+
99104
return (
100105
<RemixFormProvider {...methods}>
101-
<fetcher.Form onSubmit={methods.handleSubmit} className="w-96">
106+
<form onSubmit={methods.handleSubmit} className="w-96">
102107
<div className="space-y-6">
103108
<Select
104109
name="country"
@@ -131,7 +136,7 @@ const CascadingDropdownExample = () => {
131136
</Button>
132137
{fetcher.data?.message && <p className="mt-2 text-green-600">{fetcher.data.message}</p>}
133138
</div>
134-
</fetcher.Form>
139+
</form>
135140
</RemixFormProvider>
136141
);
137142
};
@@ -250,9 +255,14 @@ const AutoCalculationExample = () => {
250255
onChange: calculateTotal,
251256
});
252257

258+
// Don't render if methods is not ready
259+
if (!methods.handleSubmit) {
260+
return <div>Loading...</div>;
261+
}
262+
253263
return (
254264
<RemixFormProvider {...methods}>
255-
<fetcher.Form onSubmit={methods.handleSubmit} className="w-96">
265+
<form onSubmit={methods.handleSubmit} className="w-96">
256266
<div className="space-y-6">
257267
<TextField type="number" name="quantity" label="Quantity" description="Number of items" min={1} />
258268

@@ -290,7 +300,7 @@ const AutoCalculationExample = () => {
290300
</Button>
291301
{fetcher.data?.message && <p className="mt-2 text-green-600">{fetcher.data.message}</p>}
292302
</div>
293-
</fetcher.Form>
303+
</form>
294304
</RemixFormProvider>
295305
);
296306
};
@@ -398,9 +408,14 @@ const ConditionalFieldsExample = () => {
398408
},
399409
});
400410

411+
// Don't render if methods is not ready
412+
if (!methods.handleSubmit) {
413+
return <div>Loading...</div>;
414+
}
415+
401416
return (
402417
<RemixFormProvider {...methods}>
403-
<fetcher.Form onSubmit={methods.handleSubmit} className="w-96">
418+
<form onSubmit={methods.handleSubmit} className="w-96">
404419
<div className="space-y-6">
405420
<Select
406421
name="deliveryType"
@@ -441,7 +456,7 @@ const ConditionalFieldsExample = () => {
441456
</Button>
442457
{fetcher.data?.message && <p className="mt-2 text-green-600">{fetcher.data.message}</p>}
443458
</div>
444-
</fetcher.Form>
459+
</form>
445460
</RemixFormProvider>
446461
);
447462
};

packages/components/src/remix-hook-form/hooks/use-on-form-value-change.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ export const useOnFormValueChange = <
6868
const contextMethods = useRemixFormContext<TFieldValues>();
6969
const formMethods = providedMethods || contextMethods;
7070

71-
const { watch } = formMethods;
72-
7371
useEffect(() => {
74-
if (!enabled) return;
72+
// Early return if no form methods are available or hook is disabled
73+
if (!enabled || !formMethods) return;
74+
75+
const { watch } = formMethods;
7576

7677
// Subscribe to the field value changes
7778
const subscription = watch((value, { name: changedFieldName }) => {
@@ -87,5 +88,5 @@ export const useOnFormValueChange = <
8788

8889
// Cleanup subscription on unmount
8990
return () => subscription.unsubscribe();
90-
}, [name, onChange, enabled, watch, formMethods]);
91+
}, [name, onChange, enabled, formMethods]);
9192
};

0 commit comments

Comments
 (0)