Skip to content

Commit 5e4ce18

Browse files
committed
fix(react-hook-form): correctly handle unsaved changes warning (refinedev#6691)
Used useEffect to track isDirty and update setWarnWhen accordingly.
1 parent f3ba408 commit 5e4ce18

File tree

1 file changed

+5
-4
lines changed
  • packages/react-hook-form/src/useForm

1 file changed

+5
-4
lines changed

packages/react-hook-form/src/useForm/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export const useForm = <
128128
watch,
129129
setValue,
130130
getValues,
131+
formState,
131132
handleSubmit: handleSubmitReactHookForm,
132133
setError,
133134
} = useHookFormResult;
@@ -229,11 +230,11 @@ export const useForm = <
229230
return () => subscription.unsubscribe();
230231
}, [watch]);
231232

232-
const onValuesChange = (changeValues: TVariables) => {
233-
if (warnWhenUnsavedChanges) {
234-
setWarnWhen(true);
235-
}
233+
useEffect(() => {
234+
setWarnWhen(formState.isDirty && warnWhenUnsavedChanges);
235+
}, [formState.isDirty, warnWhenUnsavedChanges]);
236236

237+
const onValuesChange = (changeValues: TVariables) => {
237238
if (refineCoreProps?.autoSave?.enabled) {
238239
setWarnWhen(false);
239240

0 commit comments

Comments
 (0)