Working with Forms in React: A Beginner's Guide

Welcome to this tutorial on working with forms in React! Forms are an important part of many web applications, and working with forms in React can be a bit of a challenge for beginners. In this tutorial, we will explore the basics of working with forms in React, including how to create and handle form elements, how to validate form input, and how to submit form data.

First, let's start by creating a simple form in React. Here's an example of a form with a single text input:

AW1wb3J0IFJlYWN0IGZyb20gJ3JlYWN0JzsKCmNsYXNzIE15Rm9ybSBleHRlbmRzIFJlYWN0LkNvbXBvbmVudCB7CiAgY29uc3RydWN0b3IocHJvcHMpIHsKICAgIHN1cGVyKHByb3BzKTsKICAgIHRoaXMuc3RhdGUgPSB7CiAgICAgIG5hbWU6ICcnCiAgICB9OwogIH0KCiAgaGFuZGxlQ2hhbmdlID0gKGV2ZW50KSA9PiB7CiAgICB0aGlzLnNldFN0YXRlKHsgbmFtZTogZXZlbnQudGFyZ2V0LnZhbHVlIH0pOwogIH07CgogIGhhbmRsZVN1Ym1pdCA9IChldmVudCkgPT4gewogICAgZXZlbnQucHJldmVudERlZmF1bHQoKTsKICAgIGFsZXJ0KGBIZWxsbywgJHt0aGlzLnN0YXRlLm5hbWV9IWApOwogIH07CgogIHJlbmRlcigpIHsKICAgIHJldHVybiAoCiAgICAgIDxmb3JtIG9uU3VibWl0PXt0aGlzLmhhbmRsZVN1Ym1pdH0+CiAgICAgICAgPGxhYmVsPgogICAgICAgICAgTmFtZToKICAgICAgICAgIDxpbnB1dCB0eXBlPSJ0ZXh0IiB2YWx1ZT17dGhpcy5zdGF0ZS5uYW1lfSBvbkNoYW5nZT17dGhpcy5oYW5kbGVDaGFuZ2V9IC8+CiAgICAgICAgPC9sYWJlbD4KICAgICAgICA8YnV0dG9uIHR5cGU9InN1Ym1pdCI+U3VibWl0PC9idXR0b24+CiAgICAgIDwvZm9ybT4KICAgICk7CiAgfQp9CgpleHBvcnQgZGVmYXVsdCBNeUZvcm07

In this example, we have a class-based component called MyForm that has a single state variable called name. The name the variable is initialized to an empty string in the constructor. The handleChange method updates the name state variable with the value of the text input when the user types in the input. The handleSubmit method prevents the default form submission behavior and displays an alert with the value of the name state variable.

Now, let's say we want to validate the form input to ensure that the user has entered a name. We can do this by adding a few more pieces of logic to our form:

AW1wb3J0IFJlYWN0IGZyb20gJ3JlYWN0JzsKCmNsYXNzIE15Rm9ybSBleHRlbmRzIFJlYWN0LkNvbXBvbmVudCB7CiAgY29uc3RydWN0b3IocHJvcHMpIHsKICAgIHN1cGVyKHByb3BzKTsKICAgIHRoaXMuc3RhdGUgPSB7CiAgICAgIG5hbWU6ICcnLAogICAgICBlcnJvcjogJycKICAgIH07CiAgfQoKICBoYW5kbGVDaGFuZ2UgPSAoZXZlbnQpID0+IHsKICAgIHRoaXMuc2V0U3RhdGUoeyBuYW1lOiBldmVudC50YXJnZXQudmFsdWUgfSk7CiAgfTsKCiAgaGFuZGxlU3VibWl0ID0gKGV2ZW50KSA9PiB7CiAgICBldmVudC5wcmV2ZW50RGVmYXVsdCgpOwogICAgaWYgKHRoaXMuc3RhdGUubmFtZS50cmltKCkgPT09ICcnKSB7CiAgICAgIHRoaXMuc2V0U3RhdGUoeyBlcnJvcjogJ1BsZWFzZSBlbnRlciBhIG5hbWUnIH0pOwogICAgfSBlbHNlIHsKICAgICAgdGhpcy5zZXRTdGF0ZSh7IGVycm9yOiAnJyB9KTsKICAgICAgYWxlcnQoYEhlbGxvLCAke3RoaXMuc3RhdGUubmFtZX0hYCk7CiAgICB9CiAgfTsKCiAgcmVuZGVyKCkgewogICAgcmV0dXJuICgKICAgICAgPGZvcm0gb25TdWJtaXQ9e3RoaXMuaGFuZGxlU3VibWl0fT4KICAgICAgICA8bGFiZWw+CiAgICAgICAgICBOYW1lOgogICAgICAgICAgPGlucHV0IHR5cGU9InRleHQiIHZhbHVlPXt0aGlzLnN0YXRlLm5hbWV9IG9uQ2hhbmdlPXt0aGlzLmhhbmRsZUNoYW5nZX0gLz4KICAgICAgICAgIHt0aGlzLnN0YXRlLmVycm9yICYmIDxwIGNsYXNzTmFtZT0iZXJyb3IiPnt0aGlzLnN0YXRlLmVycm9yfTwvcD59CiAgICAgICAgPC9sYWJlbD4KICAgICAgICA8YnV0dG9uIHR5cGU9InN1Ym1pdCI+U3VibWl0PC9idXR0b24+CiAgICAgIDwvZm9ybT4KICAgICk7CiAgfQp9CgpleHBvcnQgZGVmYXVsdCBNeUZvcm07

In this updated version of MyForm, we have added a error state variable and an error message element to the form. The handleSubmit method now checks whether the name state variable is an empty string, and if it is, it sets the error state variable to an error message. If the name state variable is not empty, it sets the error state variable to an empty string and displays an alert with the value of the name state variable. The error message element is only rendered if the error state variable is not an empty string.

This is just a simple example of how you can handle and validate form input in React, but there are many other ways to approach this problem. Some other best practices to keep in mind when working with forms in React include:

  • Use controlled components to manage form input. Controlled components are components that have their values controlled by the component's state. This can make it easier to manage form input and prevent errors.

  • Use a library or framework to handle form validation. There are many libraries and frameworks available that can help you validate form input, such as Formik or React-Hook-Form.

  • Test your form components thoroughly. This can help ensure that your forms are working as intended and can be used with confidence.

We hope this tutorial has been helpful in getting you started with working with forms in React. If you have any questions or would like to learn more, there are many resources available online that can provide further guidance and information on this topic.

Want us to build yours instead?

On a project, every milestone ends with a link to the working build, and the code, the accounts and every password are yours at the handover.

What we’d build for you is on Services.

Send us the brief, or start with the free call.

Page updated .

Privacy preferences

Choose whether this browser sends visit data to our analytics tools. The site works the same either way.

Google Analytics, Amplitude and Microsoft Clarity show us which pages get read. Off means none of the three loads.

Always on

Sentry error reports, with personal data removed, and the Cloudflare spam check on the brief form.

Each cookie is listed in our Cookie Policy.