Implementation guide
Prevent duplicate form submissions in React
Add a reusable React submit guard that disables repeat sends while a request is pending and gives people a clear retry path.
By Tushar Khadde · Published · Updated
The mistake this stops
The same form or payment is sent twice while the first request is still pending.
One deliberate submission, a visible pending state, and a form that remains recoverable if the request fails.
Read the Safe Submit component docs for its live preview, API, accessibility notes, dependencies, and limitations.
React example: Safe Submit
Install the component, connect it to the action in your app, and keep the server responsible for validating and authorizing the request.
safe-submit.tsx13 lines
import { SafeSubmit } from "@/components/ui/safe-submit"
async function pay({ idempotencyKey }: { idempotencyKey: string }) {
const response = await fetch("/api/payments", {
method: "POST",
headers: { "Idempotency-Key": idempotencyKey },
})
if (!response.ok) throw new Error("Payment failed")
}
<SafeSubmit onSubmit={pay} pendingLabel="Paying…" successLabel="Paid">
Pay now
</SafeSubmit>Install the component
Use it safely
- Install the component into your shadcn project.
- Pass the async save or payment handler you already use.
- Keep idempotency and authorization checks on the server; the UI prevents accidental repeats, not malicious requests.