Cannot make the Netlify forms to work in Create React App

Hi guys,

I’ve been trying to make Netlify forms work in Create React App( https://marketic-mclbdn.netlify.app/ ) for several hours now and decided to ask here.

The form is recognized by Netlify, but when I enter some data, it’s not neither in verified submissions nor in spam submissions.

My index.html part looks like this:

<body>
    <form name="contact" netlify netlify-honeypot="bot-field" hidden>
      <input type="text" name="name" />
      <input type="email" name="email" />
      <textarea name="message"></textarea>
    </form>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>

And my ContactForm component looks like this:

import React, { useState } from "react";

const ContactForm = () => {
  const [formIsValid, setFormIsValid] = useState(false);
  const handleSubmit = (e) => {
    if (e.target.checkValidity()) {
      setFormIsValid(true);
    }
    e.preventDefault();
  };

  return (
    <form
      method="post"
      id="contact-form"
      onSubmit={handleSubmit}
      name="contact"
      data-netlify="true"
      data-netlify-honeypot="bot-field"
    >
      <input type="hidden" name="form-name" value="contact" />
      <h3>Contact Us</h3>
      {formIsValid ? (
        <p className="thank-you-para">Thank you!</p>
      ) : (
        <div>
          <fieldset>
            <input
              placeholder="NAME"
              name="name"
              type="text"
              tabindex="1"
              required
            />
          </fieldset>
          <fieldset>
            <input
              placeholder="EMAIL"
              name="email"
              type="email"
              tabindex="2"
              required
            />
          </fieldset>
          <fieldset>
            <input
              placeholder="PHONE NUMBER"
              name="phone"
              type="tel"
              tabindex="3"
              required
            />
          </fieldset>
          <fieldset>
            <textarea
              placeholder="Type your Message Here...."
              tabindex="5"
              required
              name="message"
            ></textarea>
          </fieldset>
          <fieldset>
            <button name="submit" type="submit" id="contact-submit">
              Submit
            </button>
          </fieldset>
        </div>
      )}
    </form>
  );
};

export default ContactForm;

Hey @mclbdn

The site you shared (https://happy-jennings-38380b.netlify.app/) does not seem to exist any more.

Part of the issue here is the fields in the contact form are different from those of the placeholder form in the index.html. As mentioned in the Work with JavaScript-rendered forms

…input fields with name attributes to match the inputs of your JavaScript-rendered form.

So you will need to add the phone field to the placeholder form too.

If you are interested, have a look at coelmay/netlify-react-contact which has several branches demonstrating using a form in React with Netlify.

2 Likes

Hi,

Sorry, I deleted the website and uploaded a new one ( https://marketic-mclbdn.netlify.app/ ).

Thank you for your advice about the “phone” field - I fixed that but I am still not able to receive form data.

My code now looks this:

<body>
    <form name="contact" netlify netlify-honeypot="bot-field" hidden>
      <input type="text" name="name" />
      <input type="email" name="email" />
      <input type="tel" name="phone" />
      <textarea name="message"></textarea>
    </form>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>

and

const ContactForm = () => {
  const [formIsValid, setFormIsValid] = useState(false);

  const handleSubmit = (e) => {
    if (e.target.checkValidity()) {
      setFormIsValid(true);
    }
    e.preventDefault();
  };

  return (
    <form
      name="contact"
      action="https://getform.io/f/c115e38c-178b-466c-aae0-8ed4905f400e"
      method="post"
      id="contact-form"
      onSubmit={handleSubmit}
    >
      <input type="hidden" name="form-name" value="contact" />
      <h3>Contact Us</h3>
      {formIsValid ? (
        <p className="thank-you-para">Thank you!</p>
      ) : (
        <div>
          <fieldset>
            <input
              placeholder="NAME"
              name="name"
              type="text"
              tabindex="1"
              required
            />
          </fieldset>
          <fieldset>
            <input
              placeholder="EMAIL"
              name="email"
              type="email"
              tabindex="2"
              required
            />
          </fieldset>
          <fieldset>
            <input
              placeholder="PHONE NUMBER"
              name="phone"
              type="tel"
              tabindex="3"
              required
            />
          </fieldset>
          <fieldset>
            <textarea
              placeholder="Type your Message Here...."
              tabindex="5"
              required
              name="message"
            ></textarea>
          </fieldset>
          <fieldset>
            <button name="submit" type="submit" id="contact-submit">
              Submit
            </button>
          </fieldset>
        </div>
      )}
    </form>
  );
};

export default ContactForm;

The action on the form is sending the data to getform.io, not Netlify.

If you want Netlify to handle the form, you will need to start by removing the action.

Thank you @coelmay. I also deleted the action and it’s still not working unfortunately…

Also the function…

  const handleSubmit = (e) => {
    if (e.target.checkValidity()) {
      setFormIsValid(true);
    }
    e.preventDefault();
  };

As a comparison, here is the function used in the repository I shared previously

  handleSubmit = e => {
    fetch("/", {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body: encode({ "form-name": "contact", ...this.state })
    })
      .then(() => alert("Success!"))
      .catch(error => alert(error));


    e.preventDefault();
  };

Hi, @mclbdn. The Netlify forms feature works by processing the forms field during the post processing in the build system after the build command completes.

I mention this because it means the form must exist in the HTML before any javascript is run. Our build system will process the HTML files looking for forms but it does not run any javascript. If your form requires javascript running before it is created the build system will not see the form and won’t make the backend to receive the submissions.

Lets take a look at your site’s HTML to see if that requirement is met (note: the HTML below was reformatted for readability but no other changes were made):

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <link rel="icon" href="/favicon.ico" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <meta name="theme-color" content="#000000" />
  <meta name="description" content="Web site created using create-react-app" />
  <link rel="apple-touch-icon" href="/logo192.png" />
  <link rel="manifest" href="/manifest.json" />
  <title>marketic</title>
  <script defer="defer" src="/static/js/main.7918f7c8.js"></script>
  <link href="/static/css/main.64452939.css" rel="stylesheet">
</head>

<body><noscript>You need to enable JavaScript to run this app.</noscript>
  <div id="root"></div>
</body>

</html>

As can be seen above, there is no form. Now, if you allow the javascript to run, you will see this form (again, reformatted for readability):

<form method="POST" name="contact" id="contact-form">
  <h3>Contact Us</h3>
  <div>
    <fieldset><input placeholder="NAME" name="name" type="text" tabindex="1" required="" value=""></fieldset>
    <fieldset><input placeholder="EMAIL" name="email" type="email" tabindex="2" required="" value=""></fieldset>
    <fieldset><input placeholder="PHONE NUMBER" name="phone" type="tel" tabindex="3" required="" value=""></fieldset>
    <fieldset><textarea placeholder="Type your Message Here...." tabindex="5" required="" name="message"></textarea></fieldset>
    <fieldset><button name="submit" type="submit" id="contact-submit">Submit</button></fieldset>
  </div>
</form>

The solution for this form not working is that the HTML form above must be included in the deploy. Note, you must also add a data-netlify="true" attribute to the <form> tag like so (the first line is changed below):

<form method="POST" name="contact" id="contact-form" data-netlify="true">
  <h3>Contact Us</h3>
  <div>
    <fieldset><input placeholder="NAME" name="name" type="text" tabindex="1" required="" value=""></fieldset>
    <fieldset><input placeholder="EMAIL" name="email" type="email" tabindex="2" required="" value=""></fieldset>
    <fieldset><input placeholder="PHONE NUMBER" name="phone" type="tel" tabindex="3" required="" value=""></fieldset>
    <fieldset><textarea placeholder="Type your Message Here...." tabindex="5" required="" name="message"></textarea></fieldset>
    <fieldset><button name="submit" type="submit" id="contact-submit">Submit</button></fieldset>
  </div>
</form>

If you save that HTML somewhere and deploy it to your site, the build system will see the pure HTML version of the form and create the backend service to receive these form submissions. This pure HTMTL form is always required for the Forms feature at Netlify.

Note, that pure HTML form doesn’t need to be on the same page where the form submission is. You can use your javascript generated forms to make the submissions. The pure HTML form is just required to make the backend service.

You can even put the pure HTML form in a hidden directory blocked by redirect rules so that no site visitor can ever see it. The requirement for the pure HTML form is that is exists in the deployed files somewhere. If our build system cannot see it somewhere (anywhere) in the deployed files, then form submissions won’t work.

Finally, the form above doesn’t use any honeypot field or reCAPTCHA. It is strongly recommended to use both to prevent abuse of the forms on your site. The form above will work but adding a honeypot field and reCAPTCHA would make it better.

If there are questions about any of this, please let us know.