diff --git a/packages/conform-react/context.tsx b/packages/conform-react/context.tsx index 69ece9c4..4c1679c1 100644 --- a/packages/conform-react/context.tsx +++ b/packages/conform-react/context.tsx @@ -29,6 +29,7 @@ import { useSyncExternalStore, useRef, } from 'react'; +import { flushSync } from 'react-dom'; export type Pretty = { [K in keyof T]: T[K] } & {}; @@ -484,20 +485,22 @@ export function createFormContext< return { ...context, submit(event) { - const submitEvent = event.nativeEvent as SubmitEvent; - const result = context.submit(submitEvent); - - if ( - !result.submission || - result.submission.status === 'success' || - result.submission.error === null - ) { - if (!result.formData.has(INTENT)) { - onSubmit?.(event, result); + flushSync(() => { + const submitEvent = event.nativeEvent as SubmitEvent; + const result = context.submit(submitEvent); + + if ( + !result.submission || + result.submission.status === 'success' || + result.submission.error === null + ) { + if (!result.formData.has(INTENT)) { + onSubmit?.(event, result); + } + } else { + event.preventDefault(); } - } else { - event.preventDefault(); - } + }); }, onUpdate(options) { onSubmit = options.onSubmit; diff --git a/packages/conform-react/package.json b/packages/conform-react/package.json index 323100d7..679cd136 100644 --- a/packages/conform-react/package.json +++ b/packages/conform-react/package.json @@ -49,12 +49,15 @@ "@rollup/plugin-babel": "^5.3.1", "@rollup/plugin-node-resolve": "^13.3.0", "@types/react": "^18.2.43", + "@types/react-dom": "^18.3.0", "react": "^18.2.0", + "react-dom": "^18.2.0", "rollup-plugin-copy": "^3.4.0", "rollup": "^2.79.1" }, "peerDependencies": { - "react": ">=18" + "react": ">=18", + "react-dom": ">=18" }, "keywords": [ "constraint-validation", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 53cfc858..44296e6b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -657,9 +657,15 @@ importers: '@types/react': specifier: ^18.2.43 version: 18.3.1 + '@types/react-dom': + specifier: ^18.3.0 + version: 18.3.0 react: specifier: ^18.2.0 version: 18.2.0 + react-dom: + specifier: ^18.2.0 + version: 18.2.0(react@18.2.0) rollup: specifier: ^2.79.1 version: 2.79.1