Skip to content

Commit 3aed534

Browse files
committed
fix(react-form): Non-index keys in React causing inputs to be uncontrolled when removing array fields TanStack#1363
This commit updates React to not render component when in transitory state of stores being out of sync due to array shifting
1 parent 8ef54b7 commit 3aed534

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/react-form/src/useField.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,20 @@ export const Field = (<
478478
const fieldApi = useField(fieldOptions as any)
479479

480480
const jsxToDisplay = useMemo(
481-
() => functionalUpdate(children, fieldApi as any),
481+
() => {
482+
/**
483+
* When field names switch field store and form store are out of sync.
484+
* When in this state, React should not render the component
485+
*/
486+
const isFieldStoreOutofSync =
487+
fieldApi.state.value !== fieldApi.form.getFieldValue(fieldOptions.name)
488+
489+
if (isFieldStoreOutofSync) {
490+
return null
491+
}
492+
493+
return functionalUpdate(children, fieldApi as any)
494+
},
482495
/**
483496
* The reason this exists is to fix an issue with the React Compiler.
484497
* Namely, functionalUpdate is memoized where it checks for `fieldApi`, which is a static type.

0 commit comments

Comments
 (0)