In React, forms are a crucial part of building interactive user interfaces.
Managing form data and handling form submissions involves using React state and event handling.
In React, a controlled component is a form element whose value is controlled by the React state. We maintain the form data in the component's state, and React handles the input changes through state updates.
import React, { useState } from 'react'; const YourFormComponent = () => { const [formData, setFormData] = useState({ username: '', password: '', }); const handleInputChange = (event) => { const { name, value } = event.target; setFormData({ ...formData, [name]: value }); }; const handleSubmit = (event) => { event.preventDefault(); // Handle form submission using formData console.log('Form submitted:', formData); }; return ( <form onSubmit={handleSubmit}> <label> Username: <input type="text" name="username" value={formData.username} onChange={handleInputChange} /> </label> <br /> <label> Password: <input type="password" name="password" value={formData.password} onChange={handleInputChange} /> </label> <br /> <button type="submit">Submit</button> </form> ); }; export default YourFormComponent;
import React, { useState } from 'react'; const YourCheckboxAndRadioForm = () => { const [formData, setFormData] = useState({ rememberMe: false, gender: '', }); const handleInputChange = (event) => { const { name, value, type, checked } = event.target; setFormData({ ...formData, [name]: type === 'checkbox' ? checked : value, }); }; const handleSubmit = (event) => { event.preventDefault(); // Handle form submission using formData console.log('Form submitted:', formData); }; return ( <form onSubmit={handleSubmit}> <label> Remember me: <input type="checkbox" name="rememberMe" checked={formData.rememberMe} onChange={handleInputChange} /> </label> <br /> <label> Gender: <label> <input type="radio" name="gender" value="male" checked={formData.gender === 'male'} onChange={handleInputChange} /> Male </label> <label> <input type="radio" name="gender" value="female" checked={formData.gender === 'female'} onChange={handleInputChange} /> Female </label> </label> <br /> <button type="submit">Submit</button> </form> ); }; export default YourCheckboxAndRadioForm;
In React, an uncontrolled component is a form element whose value is not controlled by the React state. Instead, we can use `React Refs` to interact with the DOM directly.
import React, { useRef } from 'react'; const YourUncontrolledForm = () => { const usernameRef = useRef(null); const passwordRef = useRef(null); const handleSubmit = (event) => { event.preventDefault(); // Access form values directly using refs console.log('Form submitted:', usernameRef.current.value, passwordRef.current.value); }; return ( <form onSubmit={handleSubmit}> <label> Username: <input type="text" ref={usernameRef} /> </label> <br /> <label> Password: <input type="password" ref={passwordRef} /> </label> <br /> <button type="submit">Submit</button> </form> ); }; export default YourUncontrolledForm;
React Hook Form Official Website: https://react-hook-form.com/
Formik Official Website: https://formik.org/