From 64d609ee43a18ccc74e9cd7fffdaaea0d2af3cc7 Mon Sep 17 00:00:00 2001 From: Roman Antipov Date: Tue, 30 Jun 2026 16:57:05 +0300 Subject: [PATCH 1/2] Fix #1093: useField read current value on the render --- src/useField.test.js | 32 ++++++++++++++++++++++++++++++++ src/useField.ts | 33 ++++++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/useField.test.js b/src/useField.test.js index 23cab6a..73df513 100644 --- a/src/useField.test.js +++ b/src/useField.test.js @@ -599,6 +599,38 @@ describe("useField", () => { expect(getByTestId("array").value).toBe("Apple"); }); + it("should return current form values on first render for newly added array fields", () => { + const renderSpy = jest.fn(); + const MyField = () => { + const { input, meta } = useField("items[0].name"); + renderSpy(input.value, meta.initial, meta.dirty); + return ; + }; + const { getByTestId } = render( +
+ {({ form, values }) => ( + + + {values.items.map((_item, index) => ( + + ))} + + )} + , + ); + + fireEvent.click(getByTestId("add")); + + expect(renderSpy.mock.calls[0]).toEqual(["Apple", undefined, true]); + expect(getByTestId("array").value).toBe("Apple"); + }); + it("should default undefined value to [] for select multiple with type prop (fix react-final-form-arrays #185)", () => { const renderSpy = jest.fn(); const MySelectField = () => { diff --git a/src/useField.ts b/src/useField.ts index db7b27a..2087298 100644 --- a/src/useField.ts +++ b/src/useField.ts @@ -116,13 +116,32 @@ function useField< const formState = form.getState(); // Use getIn to support nested field paths like "user.name" or "items[0].id" const formInitialValue = formState.initialValues ? getIn(formState.initialValues, name) : undefined; - + + const formValue = formState.values + ? getIn(formState.values, name) + : undefined; + // Use Form initialValues if available, otherwise use field initialValue let initialStateValue = formInitialValue !== undefined ? formInitialValue : initialValue; - - if ((component === "select" || type === "select") && multiple && initialStateValue === undefined) { - initialStateValue = []; + + let value = formValue !== undefined ? formValue : initialStateValue; + + if ((component === "select" || type === "select") && multiple) { + if (value === undefined && initialStateValue === undefined) { + const emptyValue: any[] = []; + value = emptyValue; + initialStateValue = emptyValue; + } else { + if (value === undefined) { + value = []; + } + if (initialStateValue === undefined) { + initialStateValue = []; + } + } } + const isEqual = configRef.current.isEqual || ((a: any, b: any) => a === b); + const pristine = isEqual(value, initialStateValue); return { active: false, @@ -133,7 +152,7 @@ function useField< form.change(name as keyof FormValues, value); }, data: data || {}, - dirty: false, + dirty: !pristine, dirtySinceLastSubmit: false, error: undefined, focus: () => { @@ -145,7 +164,7 @@ function useField< modified: false, modifiedSinceLastSubmit: false, name, - pristine: true, + pristine, submitError: undefined, submitFailed: false, submitSucceeded: false, @@ -153,7 +172,7 @@ function useField< touched: false, valid: true, validating: false, - value: initialStateValue, + value, visited: false, }; }); From fc62ed784e5c5dc42cd4059ff5abb71468643b70 Mon Sep 17 00:00:00 2001 From: Roman Antipov Date: Tue, 30 Jun 2026 19:43:34 +0300 Subject: [PATCH 2/2] Fix #1093: useField read current value on the render --- src/useField.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/useField.ts b/src/useField.ts index 2087298..eaea9c8 100644 --- a/src/useField.ts +++ b/src/useField.ts @@ -124,7 +124,12 @@ function useField< // Use Form initialValues if available, otherwise use field initialValue let initialStateValue = formInitialValue !== undefined ? formInitialValue : initialValue; - let value = formValue !== undefined ? formValue : initialStateValue; + let value = + formInitialValue !== undefined + ? formValue + : formValue !== undefined + ? formValue + : initialStateValue; if ((component === "select" || type === "select") && multiple) { if (value === undefined && initialStateValue === undefined) { @@ -133,10 +138,15 @@ function useField< initialStateValue = emptyValue; } else { if (value === undefined) { - value = []; + value = + Array.isArray(initialStateValue) && initialStateValue.length === 0 + ? initialStateValue + : []; } + if (initialStateValue === undefined) { - initialStateValue = []; + initialStateValue = + Array.isArray(value) && value.length === 0 ? value : []; } } }