File tree 1 file changed +8
-0
lines changed
packages/ui/src/forms/Form
1 file changed +8
-0
lines changed Original file line number Diff line number Diff line change @@ -93,6 +93,7 @@ export const Form: React.FC<FormProps> = (props) => {
93
93
const [ submitted , setSubmitted ] = useState ( false )
94
94
const formRef = useRef < HTMLFormElement > ( null )
95
95
const contextRef = useRef ( { } as FormContextType )
96
+ const lastChangeRef = useRef < number > ( 0 )
96
97
97
98
const fieldsReducer = useReducer ( fieldReducer , { } , ( ) => initialState )
98
99
@@ -607,11 +608,18 @@ export const Form: React.FC<FormProps> = (props) => {
607
608
if ( Array . isArray ( onChange ) ) {
608
609
let revalidatedFormState : FormState = contextRef . current . fields
609
610
611
+ const changeStartTime = new Date ( ) . getTime ( )
612
+ lastChangeRef . current = changeStartTime
613
+
610
614
for ( const onChangeFn of onChange ) {
611
615
revalidatedFormState = await onChangeFn ( {
612
616
formState : revalidatedFormState ,
613
617
} )
614
618
}
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
+ }
615
623
616
624
const { changed, newState } = mergeServerFormState (
617
625
contextRef . current . fields || { } ,
You can’t perform that action at this time.
0 commit comments