Skip to content

Commit 6631137

Browse files
author
Dinesh Agent
committed
Fix #1050: Use v6.5.9 synchronous registration approach
Restore the v6.5.9 method of getting initial state: - Temporarily disable form.destroyOnUnregister - Register field synchronously with silent=true - Capture initial state (includes Form initialValues) - Immediately unregister - Restore form.destroyOnUnregister This ensures Form initialValues are available on first render.
1 parent b114fd1 commit 6631137

1 file changed

Lines changed: 15 additions & 70 deletions

File tree

src/useField.ts

Lines changed: 15 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -98,78 +98,23 @@ function useField<
9898
// Initialize state with proper field state from Final Form without callbacks
9999
const [state, setState] = React.useState<FieldState<any>>(() => {
100100
// FIX #1050: Register field synchronously to get proper initial state
101-
// that includes Form initialValues. This ensures useField returns the
102-
// correct value on first render instead of undefined.
103-
let initialFieldState: FieldState<any> | undefined;
104-
105-
const unregister = form.registerField(
106-
name as keyof FormValues,
107-
(fieldState) => {
108-
// Capture the initial state on first registration
109-
initialFieldState = fieldState;
110-
},
111-
subscription,
112-
{
113-
afterSubmit,
114-
beforeSubmit: () => undefined,
115-
data,
116-
defaultValue,
117-
getValidator: () => config.validate,
118-
initialValue,
119-
isEqual: (a: any, b: any) => (config.isEqual || defaultIsEqual)(a, b),
120-
silent: true, // Silent registration to avoid triggering validation
121-
validateFields,
122-
}
123-
);
124-
125-
// Immediately unregister - we'll re-register properly in useEffect
126-
unregister();
127-
128-
// If we got initial state from registration, use it
129-
if (initialFieldState) {
130-
// If allowNull is true and the initial value was null, preserve it
131-
if (allowNull && initialFieldState.initial === null && initialFieldState.value !== null) {
132-
return {
133-
...initialFieldState,
134-
value: null, // Force value back to null
135-
initial: null, // Ensure our local state's 'initial' also reflects this
136-
};
137-
}
138-
return initialFieldState;
139-
}
101+
// This is the same approach used in v6.5.9 to ensure Form initialValues
102+
// are available on first render.
103+
let initialFieldState: FieldState<any> = {} as FieldState<any>;
140104

141-
// Fallback: create initial state manually (shouldn't normally reach here)
142-
let initialStateValue = initialValue;
143-
if (component === "select" && multiple && initialStateValue === undefined) {
144-
initialStateValue = [];
145-
}
105+
// Temporarily disable destroyOnUnregister
106+
const destroyOnUnregister = form.destroyOnUnregister;
107+
form.destroyOnUnregister = false;
108+
109+
// Register field synchronously with silent=true, capture state, then unregister
110+
register((fieldState) => {
111+
initialFieldState = fieldState;
112+
}, true)();
113+
114+
// Restore destroyOnUnregister to its original value
115+
form.destroyOnUnregister = destroyOnUnregister;
146116

147-
return {
148-
active: false,
149-
blur: () => { },
150-
change: () => { },
151-
data: data || {},
152-
dirty: false,
153-
dirtySinceLastSubmit: false,
154-
error: undefined,
155-
focus: () => { },
156-
initial: initialStateValue,
157-
invalid: false,
158-
length: undefined,
159-
modified: false,
160-
modifiedSinceLastSubmit: false,
161-
name,
162-
pristine: true,
163-
submitError: undefined,
164-
submitFailed: false,
165-
submitSucceeded: false,
166-
submitting: false,
167-
touched: false,
168-
valid: true,
169-
validating: false,
170-
value: initialStateValue,
171-
visited: false,
172-
};
117+
return initialFieldState;
173118
});
174119

175120
React.useEffect(() => {

0 commit comments

Comments
 (0)