Skip to content

Commit a562670

Browse files
committed
fix(ui): prevent race conditions after move row by only replacing state for the latest change
1 parent b417c1f commit a562670

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Diff for: packages/ui/src/forms/Form/index.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -605,13 +605,20 @@ export const Form: React.FC<FormProps> = (props) => {
605605
() => {
606606
const executeOnChange = async () => {
607607
if (Array.isArray(onChange)) {
608-
let revalidatedFormState: FormState = contextRef.current.fields
608+
const stateAtStartOfChange: FormState = contextRef.current.fields
609+
let revalidatedFormState = stateAtStartOfChange
609610

610611
for (const onChangeFn of onChange) {
611612
revalidatedFormState = await onChangeFn({
612613
formState: revalidatedFormState,
613614
})
614615
}
616+
// NOTE: to prevent race conditions, only replace state with the derived state from the server
617+
// if the form state has not changed since the start of executeOnChange.
618+
// Otherwise newer state data will be overwritten by the REPLACE_STATE dispatch.
619+
if (contextRef.current.fields !== stateAtStartOfChange) {
620+
return
621+
}
615622

616623
const { changed, newState } = mergeServerFormState(
617624
contextRef.current.fields || {},

0 commit comments

Comments
 (0)