React Router quickstart
Add a working contact form to a React Router app. You write the form, and Formerie checks it, stores it, and emails you.
Install
Install @formerie/react @formerie/client
Shell
npm install @formerie/react @formerie/client Shell
pnpm add @formerie/react @formerie/client Shell
yarn add @formerie/react @formerie/client Shell
bun add @formerie/react @formerie/clientFormerie packages are in alpha, so APIs can still change between releases.
Add the form
Use this as a route component, such as app/routes/contact-form.tsx.
import { useFormerieSubmit } from "@formerie/react";
export default function ContactForm() {
const { submit, state } = useFormerieSubmit({ workspace: "acme", form: "contact" });
return (
<form
onSubmit={(event) => {
event.preventDefault();
submit({ fields: Object.fromEntries(new FormData(event.currentTarget)) });
}}
>
<input name="name" placeholder="Name" required />
<input name="email" type="email" placeholder="Email" required />
<textarea name="message" placeholder="Message" required />
<button disabled={state.status === "submitting"}>Send</button>
{state.result?.ok === true && <p>Thanks!</p>}
{state.result?.ok === false && <p>{state.result.error.message}</p>}
</form>
);
}Each input’s name is the field it fills in Formerie.
Set up the form in the app
- Open app.formerie.com and create a form named Contact. It comes with the name, email, and message fields this code sends.
- On the Delivery tab, add your email address, then choose Save delivery. Confirm the email Formerie sends you.
- On the Publish tab, choose API-only and add the address your dev server prints, such as
http://localhost:5173, under Your website addresses.localhostand127.0.0.1count as different addresses. Add your live site’s address the same way when you deploy. - Choose Publish form.
- In the code above, replace
acmewith the workspace handle shown on the Publish tab.
Check it arrived
Run your app, fill in the form, and send it. “Thanks!” appears on the page. In the app, your response shows under Submissions, and its email under Deliveries.
Seeing a CORS error, or NETWORK_ERROR? Your site’s address isn’t on the Publish tab yet, or you haven’t published since adding it.
See Troubleshooting.
Going further
From your server
Use a route action, and a native form can post to it. Read what the server path costs before you choose it.
import { FormerieRuntimeClient } from "@formerie/client";
import type { Route } from "./+types/contact";
const formerie = new FormerieRuntimeClient();
export async function action({ request }: Route.ActionArgs) {
const fields = Object.fromEntries(await request.formData());
return formerie.submit("acme", "contact", { fields });
}Read next
- Recipes: quote requests, surveys, and routing by topic
- Show field errors and more
- JavaScript SDK and API: the full reference