📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn React form handling with different input field types (Input, Select, Check Box, and Radio Button).
Example of a React form with an input element
import React, { useState } from 'react';
function FormExample() {
const [name, setName] = useState('');
const handleChange = (e) => {
setName(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
console.log('Submitted name:', name);
};
return (
<div>
<h2>React Form Example</h2>
<form onSubmit={handleSubmit}>
<label htmlFor="nameInput">Name:</label>
<input
type="text"
id="nameInput"
value={name}
onChange={handleChange}
/>
<button type="submit">Submit</button>
</form>
</div>
);
}
export default FormExample;
Example of a React form with radio buttons
import React, { useState } from 'react';
function FormExample() {
const [gender, setGender] = useState('');
const handleGenderChange = (e) => {
setGender(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
console.log('Selected gender:', gender);
};
return (
<div>
<h2>React Form Example</h2>
<form onSubmit={handleSubmit}>
<label>
<input
type="radio"
name="gender"
value="male"
checked={gender === 'male'}
onChange={handleGenderChange}
/>
Male
</label>
<label>
<input
type="radio"
name="gender"
value="female"
checked={gender === 'female'}
onChange={handleGenderChange}
/>
Female
</label>
<label>
<input
type="radio"
name="gender"
value="other"
checked={gender === 'other'}
onChange={handleGenderChange}
/>
Other
</label>
<button type="submit">Submit</button>
</form>
</div>
);
}
export default FormExample;
Example of a React form with checkboxes
import React, { useState } from 'react';
function FormExample() {
const [hobbies, setHobbies] = useState([]);
const handleCheckboxChange = (e) => {
const value = e.target.value;
if (e.target.checked) {
setHobbies([...hobbies, value]);
} else {
setHobbies(hobbies.filter((hobby) => hobby !== value));
}
};
const handleSubmit = (e) => {
e.preventDefault();
console.log('Selected hobbies:', hobbies);
};
return (
<div>
<h2>React Form Example</h2>
<form onSubmit={handleSubmit}>
<label>
<input
type="checkbox"
value="reading"
checked={hobbies.includes('reading')}
onChange={handleCheckboxChange}
/>
Reading
</label>
<label>
<input
type="checkbox"
value="sports"
checked={hobbies.includes('sports')}
onChange={handleCheckboxChange}
/>
Sports
</label>
<label>
<input
type="checkbox"
value="cooking"
checked={hobbies.includes('cooking')}
onChange={handleCheckboxChange}
/>
Cooking
</label>
<button type="submit">Submit</button>
</form>
</div>
);
}
export default FormExample;
Example of a React form with a select dropdown
import React, { useState } from 'react';
function FormExample() {
const [country, setCountry] = useState('');
const handleCountryChange = (e) => {
setCountry(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
console.log('Selected country:', country);
};
return (
<div>
<h2>React Form Example</h2>
<form onSubmit={handleSubmit}>
<label htmlFor="countrySelect">Select Country:</label>
<select
id="countrySelect"
value={country}
onChange={handleCountryChange}
>
<option value="">-- Select --</option>
<option value="usa">USA</option>
<option value="canada">Canada</option>
<option value="uk">UK</option>
<option value="australia">Australia</option>
</select>
<button type="submit">Submit</button>
</form>
</div>
);
}
export default FormExample;
Complete React Form Handling Example
import React, { useState } from 'react';
function FormExample() {
const [name, setName] = useState('');
const [gender, setGender] = useState('');
const [hobbies, setHobbies] = useState([]);
const [country, setCountry] = useState('');
const handleGenderChange = (e) => {
setGender(e.target.value);
};
const handleCountryChange = (e) => {
setCountry(e.target.value);
};
const handleChange = (e) => {
setName(e.target.value);
};
const handleCheckboxChange = (e) => {
const value = e.target.value;
if (e.target.checked) {
setHobbies([...hobbies, value]);
} else {
setHobbies(hobbies.filter((hobby) => hobby !== value));
}
};
const handleSubmit = (e) => {
e.preventDefault();
console.log('Submitted name:', name);
console.log('Selected gender:', gender);
console.log('Selected hobbies:', hobbies);
console.log('Selected country:', country);
};
return (
<div className='center'>
<h2>React Form Example</h2>
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="nameInput">Name:</label>
<input
type="text"
id="nameInput"
value={name}
onChange={handleChange}
/>
</div><br />
<div>
<label>
<input
type="radio"
name="gender"
value="male"
checked={gender === 'male'}
onChange={handleGenderChange}
/>
Male
</label>
<label>
<input
type="radio"
name="gender"
value="female"
checked={gender === 'female'}
onChange={handleGenderChange}
/>
Female
</label>
<label>
<input
type="radio"
name="gender"
value="other"
checked={gender === 'other'}
onChange={handleGenderChange}
/>
Other
</label>
</div> <br />
<div>
<label>
<input
type="checkbox"
value="reading"
checked={hobbies.includes('reading')}
onChange={handleCheckboxChange}
/>
Reading
</label>
<label>
<input
type="checkbox"
value="sports"
checked={hobbies.includes('sports')}
onChange={handleCheckboxChange}
/>
Sports
</label>
<label>
<input
type="checkbox"
value="cooking"
checked={hobbies.includes('cooking')}
onChange={handleCheckboxChange}
/>
Cooking
</label>
</div>
<br />
<div>
<label htmlFor="countrySelect">Select Country:</label>
<select
id="countrySelect"
value={country}
onChange={handleCountryChange}
>
<option value="">-- Select --</option>
<option value="usa">USA</option>
<option value="canada">Canada</option>
<option value="uk">UK</option>
<option value="australia">Australia</option>
</select>
</div>
<br />
<button type="submit">Submit</button>
</form>
</div>
);
}
export default FormExample;
import FormExample from './components/FormExample'
import './App.css';
function App() {
return (
<div>
<FormExample />
</div>
)
}
export default App
.center {
width: 50%;
margin: auto;
margin-top: 50px;
border: 2px solid;
padding: 10px;
}
Related Tutorials
- React Hooks - useState and useEffect
- React JS ( React Hooks) + Spring Boot Tutorial
- Create a New React App Step by Step
- Understanding React App Folder Structure
- How to Add Bootstrap to React App
- ReactJS Axios GET, POST, PUT, and DELETE Example Tutorial
- ReactJS Axios Example
- ReactJS Fetch API Example
- React JS CRUD Example Tutorial
- React Router Step-By-Step Tutorial
- React Class Components
- React Functional Components
- React Props
- React State and setState in Class Components
- React useState Hook
- React Conditional Rendering
- How to Add Bootstrap in React JS Using NPM
- How to Create React App using Vite
- Handling Events in React
- How to Consume REST API in React
- React JS Form Validation Example
Comments
Post a Comment
Leave Comment