Skip to content

Commit 2d9ac95

Browse files
Fix useOnFormValueChange hook context handling
- Add better null checks for form methods in hook - Move hook calls after form methods validation in stories - Pass methods explicitly to avoid context issues - Ensure form is ready before calling hooks
1 parent 4d1d21a commit 2d9ac95

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,15 @@ const CascadingDropdownExample = () => {
8484
},
8585
});
8686

87+
// Don't render if methods is not ready
88+
if (!methods || !methods.handleSubmit) {
89+
return <div>Loading...</div>;
90+
}
91+
8792
// When country changes, update available states and reset state selection
8893
useOnFormValueChange({
8994
name: 'country',
95+
methods,
9096
onChange: (value) => {
9197
const states = statesByCountry[value] || [];
9298
setAvailableStates(states);
@@ -96,11 +102,6 @@ const CascadingDropdownExample = () => {
96102
},
97103
});
98104

99-
// Don't render if methods is not ready
100-
if (!methods.handleSubmit) {
101-
return <div>Loading...</div>;
102-
}
103-
104105
return (
105106
<RemixFormProvider {...methods}>
106107
<form onSubmit={methods.handleSubmit} className="w-96">
@@ -237,29 +238,32 @@ const AutoCalculationExample = () => {
237238
methods.setValue('total', total.toFixed(2));
238239
};
239240

241+
// Don't render if methods is not ready
242+
if (!methods || !methods.handleSubmit) {
243+
return <div>Loading...</div>;
244+
}
245+
240246
// Recalculate when quantity changes
241247
useOnFormValueChange({
242248
name: 'quantity',
249+
methods,
243250
onChange: calculateTotal,
244251
});
245252

246253
// Recalculate when price changes
247254
useOnFormValueChange({
248255
name: 'pricePerUnit',
256+
methods,
249257
onChange: calculateTotal,
250258
});
251259

252260
// Recalculate when discount changes
253261
useOnFormValueChange({
254262
name: 'discount',
263+
methods,
255264
onChange: calculateTotal,
256265
});
257266

258-
// Don't render if methods is not ready
259-
if (!methods.handleSubmit) {
260-
return <div>Loading...</div>;
261-
}
262-
263267
return (
264268
<RemixFormProvider {...methods}>
265269
<form onSubmit={methods.handleSubmit} className="w-96">
@@ -392,9 +396,15 @@ const ConditionalFieldsExample = () => {
392396
},
393397
});
394398

399+
// Don't render if methods is not ready
400+
if (!methods || !methods.handleSubmit) {
401+
return <div>Loading...</div>;
402+
}
403+
395404
// Show/hide fields based on delivery type
396405
useOnFormValueChange({
397406
name: 'deliveryType',
407+
methods,
398408
onChange: (value) => {
399409
setShowShipping(value === 'delivery');
400410
setShowPickup(value === 'pickup');
@@ -408,11 +418,6 @@ const ConditionalFieldsExample = () => {
408418
},
409419
});
410420

411-
// Don't render if methods is not ready
412-
if (!methods.handleSubmit) {
413-
return <div>Loading...</div>;
414-
}
415-
416421
return (
417422
<RemixFormProvider {...methods}>
418423
<form onSubmit={methods.handleSubmit} className="w-96">

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ export const useOnFormValueChange = <
7070

7171
useEffect(() => {
7272
// Early return if no form methods are available or hook is disabled
73-
if (!enabled || !formMethods) return;
73+
if (!enabled || !formMethods || !formMethods.watch || !formMethods.getValues) return;
7474

75-
const { watch } = formMethods;
75+
const { watch, getValues } = formMethods;
7676

7777
// Subscribe to the field value changes
7878
const subscription = watch((value, { name: changedFieldName }) => {
7979
// Only trigger onChange if the watched field changed
8080
if (changedFieldName === name) {
8181
const currentValue = value[name] as PathValue<TFieldValues, TName>;
8282
// Get previous value from the form state
83-
const prevValue = formMethods.getValues(name);
83+
const prevValue = getValues(name);
8484

8585
onChange(currentValue, prevValue);
8686
}

0 commit comments

Comments
 (0)