Deploying React Forms Using Formspree with Vercel

Create and deploy a React form with the help of Formspree and Vercel.
Last updated on March 16, 2023
Build, Deployment & Git

Formspree is a form backend that sends submissions via email which can connect to several third-party services such as Google Sheets, MailChimp, Slack, and more. Formspree also provides spam mitigation and filtering, file upload, automatic responses, and other tools to help you manage your forms.

In this guide, you will discover how to create a "contact us" form using React, similar to the example app, handle the form submission with Formspree and deploy it with Vercel.

Get started integrating Formspree with Vercel by using the Create a Form with Vercel page. Add the email you would like to receive form submissions at and create your form.

Formspree will first ask you to provide a password, then you will be redirected to an integration page where you can get your form URL.

Make a note of your form's endpoint. This will be used later to submit the form you build.

Go to your form's Settings to find the reCAPTCHA setting and toggle it off. To use AJAX on Formspree you must either disable reCAPTCHA or provide your own reCAPTCHA key.

Note: You can find more information on using reCAPTCHA with AJAX in the Formspree documentation.
The Settings page on Formspree.
The Settings page on Formspree.

Create a React app from your terminal with Create React App:

npm init react-app my-formspree-app && cd my-formspree-app
Creating and entering into a React app.
npm install axios
Add the axios dependency to your project:

Create a ContactForm.js file in the /src directory with the code below:

import React, { useState } from 'react';
import axios from 'axios';
export default () => {
const [status, setStatus] = useState({
submitted: false,
submitting: false,
info: { error: false, msg: null },
});
const [inputs, setInputs] = useState({
email: '',
message: '',
});
const handleServerResponse = (ok, msg) => {
if (ok) {
setStatus({
submitted: true,
submitting: false,
info: { error: false, msg: msg },
});
setInputs({
email: '',
message: '',
});
} else {
setStatus({
info: { error: true, msg: msg },
});
}
};
const handleOnChange = (e) => {
e.persist();
setInputs((prev) => ({
...prev,
[e.target.id]: e.target.value,
}));
setStatus({
submitted: false,
submitting: false,
info: { error: false, msg: null },
});
};
const handleOnSubmit = (e) => {
e.preventDefault();
setStatus((prevStatus) => ({ ...prevStatus, submitting: true }));
axios({
method: 'POST',
url: 'https://formspree.io/[your-formspree-endpoint]',
data: inputs,
})
.then((response) => {
handleServerResponse(
true,
'Thank you, your message has been submitted.',
);
})
.catch((error) => {
handleServerResponse(false, error.response.data.error);
});
};
return (
<main>
<h1>React and Formspree</h1>
<hr />
<form onSubmit={handleOnSubmit}>
<label htmlFor="email">Email</label>
<input
id="email"
type="email"
name="_replyto"
onChange={handleOnChange}
required
value={inputs.email}
/>
<label htmlFor="message">Message</label>
<textarea
id="message"
name="message"
onChange={handleOnChange}
required
value={inputs.message}
/>
<button type="submit" disabled={status.submitting}>
{!status.submitting
? !status.submitted
? 'Submit'
: 'Submitted'
: 'Submitting...'}
</button>
</form>
{status.info.error && (
<div className="error">Error: {status.info.msg}</div>
)}
{!status.info.error && status.info.msg && <p>{status.info.msg}</p>}
</main>
);
};
Adding a ContactForm.js file to the project.

This form takes two inputs, the user email and message, using axios to make the POST request with the submission data to the Formspree endpoint received earlier on.

Note: Make sure to replace your-formspree-endpoint with the endpoint received in Step 1.

Now you just need to use the form in your app. Change the contents of the App.js file to the following:

import React from 'react';
import ContactForm from './ContactForm';
export default function App() {
return (
<div>
<ContactForm />
</div>
);
}
Using the ContactForm component in the App.js file.
Note: If you wish to use the same styles as our example app, you can find them here.

To deploy your React + Formspree app with Vercel for Git, make sure it has been pushed to a Git repository.

Import the project into Vercel using your Git provider of choice.

After your project has been imported, all subsequent pushes to branches will generate Preview Deployments, and all changes made to the Production Branch (commonly "main") will result in a Production Deployment.

Form submissions received from either your localhost or production environments will be emailed, and show up in your form submissions list. If you want to restrict where the form can be embedded, set your production URL in the authorized domains setting in Formspree.

Formspree allows you to provide specially named inputs to control how form submissions are processed.

Below is a list some of the most commonly used values, but you can find all of them in the Form Setup section of the Formspree help guide.

Use this name to set the Reply-To email address of the submission notification emails.

This allows you to quickly reply to form submissions in your inbox and is shown in the example below.

<input type="email" name="_replyto" placeholder="your email address">
An example input element that uses the name="_subject" attribute to set the Reply-To of the email.

Use this name to set the subject of notification emails. This will override the default New submission from … subject.

WARNING: You should place this in a hidden input as done in the example below.
<input type="hidden" name="_subject" value="New lead on mysite.com">
An example input element that uses the name="_subject" attribute to set the subject of the email.

Couldn't find the guide you need?