Create a Contact Kind With Subsequent.js and Netlify
We’re going to create a contact kind with Subsequent.js and Netlify that shows a affirmation display screen and options enhanced spam detection.
Subsequent.js is a robust React framework for growing performant React functions that scale. By integrating a Subsequent.js website with Netlify’s know-how, we will shortly get a working contact kind up and operating with out having to jot down any server-side code.
Not solely is it a comparatively quick course of to arrange types to be processed by Netlify, but it surely’s additionally free to get began (with as much as 100 free submissions/per website hosted on Netlify). Kind submissions robotically undergo Netlify’s built-in spam filter which makes use of Akismet and there are additionally choices that may be configured to extend the extent of spam detection.

Creating the contact kind
Inside the Subsequent.js software we must always create a ContactForm
part to render the contact kind within the contact web page. In the event you’d like for this way to render at /contact
, then the ContactForm
part under with labels and enter fields ought to be used inside the pages/contact.js
file.
const ContactForm = ( <kind title="contact-form" methodology="POST" motion="contact/?success=true" > <label htmlFor="title">Identify *</label> <enter id="title" title="title" required kind="textual content" /> <label htmlFor="firm">Firm *</label> <enter id="firm" title="firm" required kind="textual content" /> <label htmlFor="e-mail">E-mail Tackle *</label> <enter id="e-mail" kind="e-mail" title="e-mail" required /> <label htmlFor="message">Message *</label> <textarea id="message" title="message" required></textarea> <button kind="submit">Submit</button> </kind>);
The above markup is required to render a kind with a subject for Identify, Firm, E-mail handle and message with a submit button. When submitting the shape, primarily based on the worth of the shape’s motion, it ought to redirect to contact/?success=true
from /contact
. Proper now there’s not but a distinction between the web page’s look with and with out the success question parameter, however we are going to replace that later.
Our Contact.js
file appears to be like like this to date:
import React from "react";const ContactPage = () => { const ContactForm = (/* code in above code pattern*/) return ( <div> <h1>Contact Us</h1> {ContactForm} </div> );}; export default ContactPage;
Now that we’ve got the fundamental kind arrange, the actual magic will occur after we add extra data for Netlify to auto-recognize the shape throughout future website deployments. To perform this we must always replace the shape to have the attribute data-netlify="true"
and a hidden enter subject that accommodates the title of our contact kind. In Netlify, as soon as we navigate to our website within the dashboard after which click on on the “types” tab we can view our kind responses primarily based on the title that we’ve put in our hidden subject. It’s necessary that if in case you have a number of types inside a website that they’ve distinctive names in order that they’re recorded correctly in Netlify.
<kind methodology="POST" title="contact-form" motion="contact/?success=true" data-netlify="true"><enter kind="hidden" title="form-name" worth="contact-form" />
After efficiently deploying the location to Netlify with the data-netlify
attribute and the form-name
subject then we will go to the deployed model of the location and fill out the shape. Upon submitting the shape and navigating to https://app.netlify.com/websites/site-name/types
(the place site-name
is the title of your website) then our most up-to-date kind submission ought to seem if we’ve got efficiently arrange the shape.

Redirect to affirmation display screen
As a way to enhance the consumer expertise, we must always add some logic to redirect to a affirmation display screen on kind submission when the URL modifications to /contact/?success=true
. There’s additionally the choice to redirect to a wholly totally different web page because the motion when the shape is submitted however utilizing question params we will obtain one thing related with the Subsequent Router. We are able to accomplish this by creating a brand new variable to find out if the affirmation display screen or the shape ought to be seen primarily based on the question parameter. The following/router which is imported with import { useRouter } from "subsequent/router";
can be utilized to retrieve the present question params.
const router = useRouter(); const confirmationScreenVisible = router.question?.success && router.question.success === "true";
In our case, the affirmation display screen and kind can by no means be seen on the identical time; due to this fact, the next assertion can be utilized to find out if the shape is seen or not.
const formVisible = !confirmationScreenVisible;
To provide customers the choice to resubmit the shape, we will add a button to the affirmation display screen to reset the shape by clearing the question params. Utilizing router.substitute
(as an alternative of router.push
) not solely updates the web page however replaces the present web page within the historical past to the model with out question params.
<button onClick={() => router.substitute("/contact", undefined, { shallow: true })}> Submit One other Response </button>
We are able to then conditionally render the shape primarily based on whether or not or not the shape is seen with:
{formVisible ? ContactForm : ConfirmationMessage}
Placing all of it collectively, we will use the next code to conditionally render the shape primarily based on the question params (that are up to date when the shape is submitted):
import React, { useState } from "react";import { useRouter } from "subsequent/router"; const ContactPage = () => { const [submitterName, setSubmitterName] = useState(""); const router = useRouter(); const confirmationScreenVisible = router.question?.success && router.question.success === "true"; const formVisible = !confirmationScreenVisible; const ConfirmationMessage = ( <React.Fragment> <p> Thanks for submitting this way. Somebody ought to get again to you inside 24-48 hours. </p> <button onClick={() => router.substitute("/contact", undefined, { shallow: true })}> Submit One other Response </button> </React.Fragment> ); const ContactForm = (/* code in first code instance */); return ( <div> <h1>Contact Us</h1>{formVisible ? ContactForm : ConfirmationMessage} </div> );}; export default ContactPage;
Including a hidden bot subject
Now that the core performance of our kind is working, we will add extra spam detection to our kind along with the bottom spam detection as a result of Akismet is included with all Netlify Kinds by default. We are able to allow this by including data-netlify-honeypot="bot-field"
to our kind.
<kind className="container" methodology="POST" title="contact-form" motion="contact/?success=true" data-netlify="true" data-netlify-honeypot="bot-field">
We additionally have to create a brand new hidden paragraph that accommodates a label named bot-field
that accommodates the enter. This subject is “seen” to bots, however not people. When this honeypot kind subject is stuffed, Netlify detects a bot after which the submission is flagged as spam.
<p hidden> <label> Don’t fill this out: <enter title="bot-field" /> </label></p>
Additional customizations
- We might discover one other spam prevention possibility that Netlify helps by including reCAPTCHA 2 to a Netlify kind.
- We might replace the shape to allow uploaded files with enter
<enter kind="file">
. - We might arrange notifications for kind submissions. That occurs over at
https://app.netlify.com/websites/[your-site-name]/settings/types
the place we will embrace a customized topic subject (which may be hidden) for e-mail notifications.

Full code
The code for the total website code is available over at GitHub.
Bonus
The next code contains all the things we coated in addition to the logic for setting a customized topic line with what was submitted within the title subject.
import React, { useState } from "react";import { useRouter } from "subsequent/router"; const ContactPage = () => { const [submitterName, setSubmitterName] = useState(""); const router = useRouter(); const confirmationScreenVisible = router.question?.success && router.question.success === "true"; const formVisible = !confirmationScreenVisible; const ConfirmationMessage = ( <React.Fragment> <p> Thanks for submitting this way. Somebody ought to get again to you inside 24-48 hours. </p> <button onClick={() => router.substitute("/contact", undefined, { shallow: true })}> Submit One other Response </button> </React.Fragment> ); const ContactForm = ( <kind className="container" methodology="POST" title="contact-form" motion="contact/?success=true" data-netlify="true" data-netlify-honeypot="bot-field" > <enter kind="hidden" title="topic" worth={`You have obtained mail from ${submitterName}`} /> <enter kind="hidden" title="form-name" worth="contact-form" /> <p hidden> <label> Don’t fill this out: <enter title="bot-field" /> </label> </p> <label htmlFor="title">Identify *</label> <enter id="title" title="title" required onChange={(e) => setSubmitterName(e.goal.worth)} kind="textual content" /> <label htmlFor="firm">Firm *</label> <enter id="firm" title="firm" required kind="textual content" /> <label htmlFor="e-mail">E-mail Tackle *</label> <enter id="e-mail" kind="e-mail" title="e-mail" required /> <label htmlFor="message">Message *</label> <textarea id="message" title="message" required/> <button kind="submit">Submit</button> </kind> ); return ( <div> <h1>Contact Us</h1>{formVisible ? ContactForm : ConfirmationMessage} </div> );}; export default ContactPage;
Checkout extra Articles on Sayed.CYou
Comments
Post a Comment