File tree 1 file changed +8
-1
lines changed
packages/ui/src/forms/Form
1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -605,13 +605,20 @@ export const Form: React.FC<FormProps> = (props) => {
605
605
( ) => {
606
606
const executeOnChange = async ( ) => {
607
607
if ( Array . isArray ( onChange ) ) {
608
- let revalidatedFormState : FormState = contextRef . current . fields
608
+ const stateAtStartOfChange : FormState = contextRef . current . fields
609
+ let revalidatedFormState = stateAtStartOfChange
609
610
610
611
for ( const onChangeFn of onChange ) {
611
612
revalidatedFormState = await onChangeFn ( {
612
613
formState : revalidatedFormState ,
613
614
} )
614
615
}
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
+ }
615
622
616
623
const { changed, newState } = mergeServerFormState (
617
624
contextRef . current . fields || { } ,
You can’t perform that action at this time.
0 commit comments