Skip to content

Commit 015c17f

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

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const Form: React.FC<FormProps> = (props) => {
9393
const [submitted, setSubmitted] = useState(false)
9494
const formRef = useRef<HTMLFormElement>(null)
9595
const contextRef = useRef({} as FormContextType)
96+
const lastChangeRef = useRef<number>(0)
9697

9798
const fieldsReducer = useReducer(fieldReducer, {}, () => initialState)
9899

@@ -607,11 +608,18 @@ export const Form: React.FC<FormProps> = (props) => {
607608
if (Array.isArray(onChange)) {
608609
let revalidatedFormState: FormState = contextRef.current.fields
609610

611+
const changeStartTime = new Date().getTime()
612+
lastChangeRef.current = changeStartTime
613+
610614
for (const onChangeFn of onChange) {
611615
revalidatedFormState = await onChangeFn({
612616
formState: revalidatedFormState,
613617
})
614618
}
619+
// NOTE: to prevent race conditions, only replace state from server when no newer change has been executed
620+
if (lastChangeRef.current !== changeStartTime) {
621+
return
622+
}
615623

616624
const { changed, newState } = mergeServerFormState(
617625
contextRef.current.fields || {},

0 commit comments

Comments
 (0)