bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/React/React Core
React•React Core

React Forms

Just like in HTML, React uses forms to allow users to interact with the web page.

Adding Forms in React

You add a form with React like any other element:

Example

Add a form that allows users to enter their name:

function MyForm() {
 return ( <form> <label>Enter your name: <input type="text" /> </label> </form> )
}
createRoot(document.getElementById('root')).render( <MyForm /> );

This will work as normal, the form will submit and the page will refresh.

But this is generally not what we want to happen in React.

We want to prevent this default behavior and let React control the form.

HTML Forms vs. React Forms

In React, form elements like <input> , <textarea> , and <select> work a bit differently from traditional HTML.

In standard HTML, form elements maintain their own value based on user input.

For example, an <input type="text"> field keeps track of its own value in the HTML DOM.

In React, the value of the form element is kept in the component's state property and updated only with the setState () function.

In other words; React provides a way to manage form data through component state, leading to what are known as "controlled components."

Controlled Components

In a controlled component, form data is handled by the React component.

The value of the input element is driven by the React state , and any changes to that value are managed through event handlers that update the state.

When the data is handled by the components, all the data is stored in the component state.

We can use the useState Hook to keep track of each input value and provide a "single source of truth" for the entire application.

See the React Hooks section for more information on Hooks.

Example

Use the useState Hook to manage the input:

import { useState } from 'react';
import { createRoot } from 'react-dom/client';
function MyForm() {
 const [name, setName] = useState("");
 function handleChange(e) {
 setName(e.target.value);
 }
 return ( <form> <label>Enter your name: <input
 type="text"
 value={name}
 onChange={handleChange}
 /> </label> <p>Current value: {name}</p> </form> )
}
createRoot(document.getElementById('root')).render( <MyForm /> );

Example Explained

  1. Import the useState Hook from React:
import { useState } from 'react';
  1. Declare a state variable to hold the input's value and a function to update it:
const [name, setName] = useState("");
  1. Create a function to handle the change event:
function handleChange(e) {
 setName(e.target.value);
}
  1. Set the value of the input field to the state variable and the onChange attribute to handle the change event:
<input
type="text"
value={name}
onChange={handleChange}
/>
  1. Display the current value to show that the value is being updated:
<p>Current value: {name}</p>

Initial Values

To add an initial value to the input field in the example above, add a value to the useState object:

Use initial value for name

function MyForm() {
 const [name, setName] = useState("John");
 ...

Previous

React Lists

Next

React Submit Forms

This chapter

Overview
45

Lessons

142m

Read time

1. React Tutorial2. React Introduction3. React Getting Started4. Your First React App5. React Render HTML6. Upgrade React7. React ES68. React ES6 Classes9. React ES6 Arrow Functions10. React ES6 Variables11. JavaScript Array map()12. React ES6 Destructuring13. React ES6 Spread Operator14. React ES6 Modules15. React ES6 Ternary Operator16. React ES6 Template Strings17. React JSX18. React JSX Expressions19. React JSX Attributes20. React JSX If Statements21. React Components22. React Class Components23. React Props24. React Destructuring Props25. React Props Children26. React Events27. React Conditional Rendering28. React Lists29. React Forms30. React Submit Forms31. React Forms - Textarea32. React Forms - Select33. React Forms - Multiple Input Fields34. React Forms - Checkbox35. React Forms - Radio36. React Portals37. React Suspense38. Styling React Using CSS39. React CSS Modules40. React CSS-in-JS41. React Router42. React Transitions43. React forwardRef44. React Higher Order Components45. React Sass Styling

On this page

Adding Forms in ReactHTML Forms vs. React FormsControlled ComponentsInitial Values