Loading page
React integration
React forms keep their own state and validation. Formivo only replaces the destination of the request, so nothing about the component's design changes.
Keep your controlled React form and POST the values as JSON from the submit handler. You do not need a server, an SDK or a key. Handle 201 as success and surface 409 and 429 as distinct messages.
"use client";
import { useState } from "react";
export function ContactForm() {
const [status, setStatus] = useState<"idle" | "sending" | "sent" | "error">("idle");
async function onSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
setStatus("sending");
const data = Object.fromEntries(new FormData(event.currentTarget));
const response = await fetch(
"https://formivo.faizyab.me/api/f/YOUR_FORM_ID",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
},
);
setStatus(response.status === 201 ? "sent" : "error");
}
return (
<form onSubmit={onSubmit}>
<input name="name" required />
<input name="email" type="email" required />
<button disabled={status === "sending"}>
{status === "sending" ? "Sending…" : "Send"}
</button>
{status === "sent" ? <p>Thanks, your message arrived.</p> : null}
{status === "error" ? <p>That did not send. Please try again.</p> : null}
</form>
);
}