🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to React Notes
Topic #29

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:

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.

Note: See the React Hooks section for more information on Hooks.

Example:

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:

Example:

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

  ...

Want to go beyond the notes?

Join CodingNow 2.0's React course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

React Forms – FAQs

Quick answers about learning React Forms in React.

This free note from CodingNow 2.0 explains React Forms in React — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every React topic on CodingNow 2.0, including React Forms, is 100% free with no signup required.
With focused practice, most students grasp React Forms in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now