Netlify processes form submissions without any server-side code on your side, which makes it a natural pairing with a statically built or server-rendered Next.js site. Getting started is free, with up to 100 submissions per site hosted on Netlify. Every submission passes through Netlify's built-in spam filter, which relies on Akismet, and additional filters can be configured on top of it.

Building the form component

Create a ContactForm component so the form can be rendered inside the contact page. To serve the form at /contact, the label and input markup below belongs in pages/contact.js.

const ContactForm = (
  <form
    name="contact-form"
    method="POST"
    action="contact/?success=true"
  >
    <label htmlFor="name">Name *</label>
    <input
      id="name"
      name="name"
      required
      type="text"
    />
    <label htmlFor="company">Company *</label>
    <input id="company" name="company" required type="text" />
    <label htmlFor="email">E-mail Address *</label>
    <input id="email" type="email" name="email" required />
    <label htmlFor="message">Message *</label>
    <textarea id="message" name="message" required></textarea>
    <button type="submit">Submit</button>
  </form>
);

This produces fields for Name, Company, Email address and a message, along with a submit button. On submission, the form's action value redirects the browser from /contact to contact/?success=true. At this point the page looks the same with or without the query parameter; that changes below.

The Contact.js file so far:

import React from "react";
const ContactPage = () => {
 const ContactForm = (/* code in above code sample*/)
 
 return (
   <div>
     <h1>Contact Us</h1>
     {ContactForm}
   </div>
 );
};
 
export default ContactPage;

Making Netlify recognize the form

For Netlify to auto-detect the form on future deployments, add data-netlify="true" to the form element and include a hidden input carrying the form's name. In the Netlify dashboard, the "forms" tab lists responses under the name given in that hidden field. If a site has several forms, each name must be unique so submissions are recorded against the right one.

<form
  method="POST"
  name="contact-form"
  action="contact/?success=true"
 
>
<input type="hidden" name="form-name" value="contact-form" />

Once the site is deployed with the data-netlify attribute and the form-name field in place, fill out the live form and open https://app.netlify.com/sites/site-name/forms (substituting your site name). The newest submission should be listed there.

Confirmation screen on success

When the URL becomes /contact/?success=true, the page should show a confirmation screen instead of the form. Redirecting to a separate page as the form action is another option, but query parameters combined with the Next router achieve a similar result. A variable decides which of the two views is visible, using the router imported with import { useRouter } from "next/router"; to read the current query parameters.

const router = useRouter();  
const confirmationScreenVisible = router.query?.success && router.query.success === "true";

Since the confirmation screen and the form are never shown together, one condition is enough to switch between them:

const formVisible = !confirmationScreenVisible; 

To let users submit again, add a button on the confirmation screen that resets the form by clearing the query parameters. Calling router.replace rather than router.push updates the page and swaps the current history entry for the URL without the query parameters.

<button onClick={() => router.replace("/contact", undefined, { shallow: true })}> Submit Another Response </button>

The form is then rendered conditionally on that visibility flag:

{formVisible ? ContactForm : ConfirmationMessage}

The full conditional rendering, driven by the query parameters that change at submission time:

import React, { useState } from "react";
import { useRouter } from "next/router";
 
const ContactPage = () => {
 const [submitterName, setSubmitterName] = useState("");
 const router = useRouter();
 const confirmationScreenVisible =
   router.query?.success && router.query.success === "true";
 const formVisible = !confirmationScreenVisible;
 
 const ConfirmationMessage = (
   <React.Fragment>
     <p>
       Thank you for submitting this form. Someone should get back to you within 24-48 hours.
     </p>
 
     <button onClick={() => router.replace("/contact", undefined, { shallow: true })}> Submit Another Response </button>
   </React.Fragment>
 );
 
 const ContactForm = (/* code in first code example */);
 
 return (
   <div>
     <h1>Contact Us</h1>
{formVisible ? ContactForm : ConfirmationMessage}
   </div>
 );
};
 
export default ContactPage;

Extra spam protection with a honeypot

Netlify Forms include Akismet-based filtering by default; a honeypot field adds another layer. Enable it with data-netlify-honeypot="bot-field" on the form.

<form
  className="container"
  method="POST"
  name="contact-form"
  action="contact/?success=true"
 
 
>

Also add a hidden paragraph holding a label named bot-field that wraps the input. Bots can see this field, humans cannot. If it arrives filled in, Netlify treats the submission as spam.

<p hidden>
  <label>
    Don’t fill this out: <input name="bot-field" />
  </label>
</p>

Where to go next

  • Add reCAPTCHA 2, another spam option Netlify supports for its forms.
  • Accept file uploads by adding an <input type="file"> input.
  • Configure submission notifications at https://app.netlify.com/sites/[your-site-name]/settings/forms, where a custom subject line for the email can also be set (and hidden).

The complete site code is available on GitHub.

Full example with a custom subject

The listing below contains everything covered above plus logic that builds a custom subject line from what was entered in the name field.

import React, { useState } from "react";
import { useRouter } from "next/router";
 
const ContactPage = () => {
 const [submitterName, setSubmitterName] = useState("");
 const router = useRouter();
 const confirmationScreenVisible =
   router.query?.success && router.query.success === "true";
 const formVisible = !confirmationScreenVisible;
 
 const ConfirmationMessage = (
   <React.Fragment>
     <p>
       Thank you for submitting this form. Someone should get back to you
       within 24-48 hours.
     </p>
 
     <button onClick={() => router.replace("/contact", undefined, { shallow: true })}> Submit Another Response </button>
   </React.Fragment>
 );
 
 const ContactForm = (
   <form
     className="container"
     method="POST"
     name="contact-form"
     action="contact/?success=true"
    
    
   >
     <input
       type="hidden"
       name="subject"
       value={`You've got mail from ${submitterName}`}
     />
     <input type="hidden" name="form-name" value="contact-form" />
     <p hidden>
       <label>
         Don’t fill this out: <input name="bot-field" />
       </label>
     </p>
 
     <label htmlFor="name">Name *</label>
     <input
       id="name"
       name="name"
       required
       onChange={(e) => setSubmitterName(e.target.value)}
       type="text"
     />
     <label htmlFor="company">Company *</label>
     <input id="company" name="company" required type="text" />
     <label htmlFor="email">E-mail Address *</label>
     <input id="email" type="email" name="email" required />
     <label htmlFor="message">Message *</label>
     <textarea id="message" name="message" required/>
     <button type="submit">Submit</button>
   </form>
 );
 
 return (
   <div>
     <h1>Contact Us</h1>
{formVisible ? ContactForm : ConfirmationMessage}
   </div>
 );
};
 
export default ContactPage;