Netlify comes with some handy, built-in features to process form submissions without having to write any server-side code. Form handling was historically a paid feature but, with the new super-powered free tier, it is now available for all sites for free. 🎉
If your site includes an HTML form, you can add a netlify (or data-netlify="true") attribute to the form tag and start receiving submissions right away (learn more in the form handling docs.
In a React app, however, just adding a netlify attribute to a JSX form won’t work [sad trombone]. Note: If you are using a static site generator like Gatsby or React-Static, it will work but requires a form-name field, so skip to the Static Site Generator section below for examples.
The post-processing bots that look for the netlify attributes when a site is deployed only know how to parse HTML. Until they evolve enough to detect forms rendered with JavaScript, we need to give them a little extra help.
Form Handling with a Stateless React Form
For simplicity’s sake, we’ll use React’s single file example as starting point for our very basic React contact form.
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <title>Contact</title> <script src="https://unpkg.com/react@16/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> </head> <body> <div id="root"></div> <script type="text/babel">
ReactDOM.render( <form name="contact" method="post"> <p> <label>Your Name: <input type="text" name="name"/></label> </p> <p> <label>Your Email: <input type="email" name="email"/></label> </p> <p> <label>Message: <textarea name="message"></textarea></label> </p> <p> <button type="submit">Send</button> </p> </form>, document.getElementById("root") );
</script> </body></html>