React Form Handling using useState (Controlled Components)

๐Ÿ“Œ INTRODUCTION

Handling forms in React is different from traditional HTML. In React, we use controlled components, where form inputs are controlled by state.

In this program, we will:

  • Use useState to manage form inputs
  • Handle user input dynamically
  • Understand controlled components

๐Ÿ‘‰ This concept is essential for:

  • Login forms
  • Registration systems
  • Real-world applications

๐Ÿ“˜ PROBLEM STATEMENT

๐Ÿ‘‰ Create a program to manage a simple form with input fields and a submit button using state in React.


๐Ÿ’ก CONCEPTS USED

  • useState Hook
  • Controlled Components
  • Event Handling
  • Form Submission

๐Ÿง  LOGIC EXPLANATION

  1. Create state for form inputs
  2. Bind input value to state
  3. Update state on change
  4. Handle submit event

๐Ÿ’ป PROGRAM CODE

Open src/App.js:

import { useState } from "react";

function App() {
const [formData, setFormData] = useState({
name: "",
email: ""
});

const handleChange = (e) => {
setFormData({
...formData,
[e.target.name]: e.target.value
});
};

const handleSubmit = (e) => {
e.preventDefault();
console.log(formData);
alert(`Name: ${formData.name}, Email: ${formData.email}`);
};

return (
<div style={{ textAlign: "center", marginTop: "50px" }}>
<h1>Controlled Form</h1>

<form onSubmit={handleSubmit}>
<input
type="text"
name="name"
placeholder="Enter Name"
value={formData.name}
onChange={handleChange}
/>

<br /><br />

<input
type="email"
name="email"
placeholder="Enter Email"
value={formData.email}
onChange={handleChange}
/>

<br /><br />

<button type="submit">Submit</button>
</form>
</div>
);
}

export default App;

๐Ÿ–ฅ๏ธ OUTPUT

๐Ÿ‘‰ User enters:

Name: Kavita
Email: kavita@gmail.com

๐Ÿ‘‰ After submit:

Alert: Name: Kavita, Email: kavita@gmail.com

๐Ÿ” STEP-BY-STEP EXPLANATION

  • useState() โ†’ stores form data
  • value={formData.name} โ†’ controlled input
  • onChange โ†’ updates state
  • handleSubmit() โ†’ processes form

๐ŸŽฏ KEY CONCEPT: CONTROLLED COMPONENT

๐Ÿ‘‰ In React:

  • Input value is controlled by state
  • State is the single source of truth

โšก ADVANCED FEATURES

1. Add Password Field

<input type="password" name="password" />

2. Add Validation

if (!formData.name) {
alert("Name is required");
}

3. Reset Form

setFormData({ name: "", email: "" });

4. Handle Multiple Fields

๐Ÿ‘‰ Same logic works for any number of inputs


๐ŸŒŸ ADVANTAGES

  • Full control over inputs
  • Easy validation
  • Better user experience
  • Required for real-world apps

๐Ÿ“š REAL-WORLD USE CASE

  • Login forms
  • Registration forms
  • Payment forms
  • Feedback systems

โ“ VIVA QUESTIONS (IMPORTANT)

Q1. What is a controlled component?

๐Ÿ‘‰ A component whose input values are controlled by React state.


Q2. Why use useState in forms?

๐Ÿ‘‰ To manage and update input values.


Q3. What is the role of onChange?

๐Ÿ‘‰ Updates state when user types.


Q4. What is event.preventDefault()?

๐Ÿ‘‰ Prevents page reload on form submission.


Q5. What is uncontrolled component?

๐Ÿ‘‰ Inputs not controlled by state.


๐Ÿ”— RELATED POSTS

๐Ÿ‘‰ MCA 168 Full Stack Development Lab Programs List
๐Ÿ‘‰ React Simple Form โ€“ Program 14
๐Ÿ‘‰ React Fetch API โ€“ Program 15
๐Ÿ‘‰ React Props โ€“ Program 10


๐Ÿ“Œ CONCLUSION

Form handling using useState is a fundamental concept in React. Controlled components give you complete control over user input.

๐Ÿ‘‰ Master this to build professional and real-world applications.


Further Reading

JUnit Tutorial

How to Master Full Stack Development?

Spring Framework Practice Problems and Their Solutions

30 MCQs on JUnit

From Google to the World: The Story of Go Programming Language

Why Go? Understanding the Advantages of this Emerging Language

Creating and Executing Simple Programs in Go

20+ Interview Questions on Go Programming Language

100+ MCQs On Java Architecture

Java Practice Exercise

programmingempire

Princites

Leave a Reply

Your email address will not be published. Required fields are marked *