March 3, 2024

Md. Saad

Formspree is a versatile tool that simplifies form handling for developers with HTML, JavaScript, and React forms. It is a backend service, handling form processing and storage without requiring server-side code. Formspree allows you to embed custom contact, order, or email capture forms on your static website. It sends email notifications and auto-responses, offers spam protection, and integrates with third-party services. In this article, we will discuss a step-by-step guide on how to add a contact form to your Next.js website using Formspree:
Prerequisites
npx create-next-app <app-name>Hopefully, you have already created a Formspree account. If you don’t have one yet, do it now.


The integration choices for your new Formspree form will then be shown to you. Keep in mind that Formspree offers you a variety of implementation samples, including ordinary HTML, Ajax, and React.
project-root/
├─ next.config.js
├─ components/
| └─ contact-form.js
└─ pages/
└─ index.js npm install --save @formspree/react import { useForm, ValidationError } from "@formspree/react";
export default function ContactForm() {
const [state, handleSubmit] = useForm("YOUR_FORM_ID");
if (state.succeeded) {
return <p>Thanks for your submission!</p>;
}
return (
<form onSubmit={handleSubmit}>
<label htmlFor="email">Email Address</label>
<input id="email" type="email" name="email" />
<ValidationError prefix="Email" field="email" errors={state.errors} />
<textarea id="message" name="message" />
<ValidationError prefix="Message" field="message" errors={state.errors} />
<button type="submit" disabled={state.submitting}>Submit</button>
<ValidationError errors={state.errors} />
</form>
);
} import ContactForm from "../components/contact-form"; <ContactForm />That’s it you have successfully added Formspree to your Nexr.js project. BUt there is something more that we can do. Such as creating a form endpoint with Vercel. Let’s discuss it.





const [state, handleSubmit] = useForm(process.env.NEXT_PUBLIC_FORM);Now, when you fill out the form and submit it, you should see a success message. That’s it, you’re done! This guide should help you have a working React contact form in your Next.js project that sends you email notifications. The project repository is hosted on GitHub.
Want to integrate Formspree form in your Next.js project? Contact us! Our Next.js team will help you in all possible ways.