diff --git a/modules/core/src/web/hooks/use-hook-form/use-hook-form.ts b/modules/core/src/web/hooks/use-hook-form/use-hook-form.ts index 1fd33a75..5474fcbb 100644 --- a/modules/core/src/web/hooks/use-hook-form/use-hook-form.ts +++ b/modules/core/src/web/hooks/use-hook-form/use-hook-form.ts @@ -1,5 +1,5 @@ import { zodResolver } from "@hookform/resolvers/zod"; -import { useEffect } from "react"; +import { useEffect, useRef } from "react"; import { FieldValues, UseFormProps, UseFormReturn, useForm } from "react-hook-form"; import * as z4 from "zod/v4/core"; @@ -28,20 +28,40 @@ export function useHookForm(undefined); + useEffect(() => { onDirtyChange?.(isDirty); }, [isDirty, onDirtyChange]); - useEffect(() => { + /*useEffect(() => { const applyReset = async () => { const values = typeof initialValues === "function" ? await initialValues() : initialValues; form.reset(values); }; applyReset(); - }, [initialValues, form]); + }, [initialValues, form]);*/ + + useEffect(() => { + let mounted = true; + const apply = async () => { + const next = typeof initialValues === "function" ? await initialValues() : initialValues; + // Evita reset redundante + if (mounted && JSON.stringify(next) !== JSON.stringify(lastAppliedRef.current)) { + reset(next as TFields); + lastAppliedRef.current = next; + } + }; + void apply(); + return () => { + mounted = false; + }; + }, [initialValues, reset]); return form; }