Skip to content

Commit 171d2ef

Browse files
author
Erik Rasmussen
committed
fix: always advance prevInitialValueRef regardless of undefined
Previously the ref was only updated inside the 'initialValue !== undefined' branch, so a transition like "foo" → undefined → "foo" would leave the ref stuck at "foo" and the second change would look like a no-op, leaving dirty/pristine state stale. Move the ref update unconditionally before the condition check, while keeping the registration logic gated on 'initialValue !== undefined'. Addresses CodeRabbit review comment.
1 parent 4fa6600 commit 171d2ef

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/useField.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,12 @@ function useField<
197197
React.useEffect(() => {
198198
// Use the configured isEqual function (respects custom equality for objects/arrays)
199199
const isEqual = configRef.current.isEqual || ((a: any, b: any) => a === b);
200-
// Only run when initialValue actually changes (not on mount)
201-
if (
202-
!isEqual(prevInitialValueRef.current, initialValue) &&
203-
initialValue !== undefined
204-
) {
205-
prevInitialValueRef.current = initialValue;
200+
const prevInitialValue = prevInitialValueRef.current;
201+
// Always advance the ref so transitions through `undefined` are tracked
202+
// correctly (e.g. "foo" → undefined → "foo" must re-trigger the block).
203+
prevInitialValueRef.current = initialValue;
204+
// Only run when initialValue actually changes (not on mount) and is defined
205+
if (!isEqual(prevInitialValue, initialValue) && initialValue !== undefined) {
206206

207207
// Get current form state
208208
const formState = form.getState();

0 commit comments

Comments
 (0)