Forms are an essential part of web applications, allowing users to input data and interact with your application. React provides powerful ways to handle forms, making user input management both flexible and efficient.
In React, forms work differently than traditional HTML forms. Instead of letting the browser handle form submissions and page refreshes, React typically takes control of the process, providing a smoother user experience and more powerful data handling capabilities.
Here's a simple example of a form in React:
1import React, { useState } from "react"; 2 3function SimpleForm() { 4 const [formData, setFormData] = useState({ 5 username: "", 6 email: "" 7 }); 8 9 const handleChange = (event) => { 10 const { name, value } = event.target; 11 setFormData({ 12 ...formData, 13 [name]: value 14 }); 15 }; 16 17 const handleSubmit = (event) => { 18 event.preventDefault(); 19 console.log("Form submitted with data:", formData); 20 // Form processing logic here 21 }; 22 23 return ( 24 <form onSubmit={handleSubmit}> 25 <div> 26 <label htmlFor="username">Username:</label> 27 <input 28 type="text" 29 id="username" 30 name="username" 31 value={formData.username} 32 onChange={handleChange} 33 /> 34 </div> 35 36 <div> 37 <label htmlFor="email">Email:</label> 38 <input 39 type="email" 40 id="email" 41 name="email" 42 value={formData.email} 43 onChange={handleChange} 44 /> 45 </div> 46 47 <button type="submit">Submit</button> 48 </form> 49 ); 50}
In this example:
useState
hook to create form statehandleChange
updates the state when input values changehandleSubmit
prevents the default form submission and processes the datavalue
prop and onChange
handlerWhen working with forms in React, the submission process typically follows these steps:
onSubmit
handler function is calledevent.preventDefault()
React offers two approaches to handling form inputs:
Controlled components are form elements whose values are controlled by React state:
1function ControlledInput() { 2 const [value, setValue] = useState(""); 3 4 return ( 5 <input 6 value={value} 7 onChange={(e) => setValue(e.target.value)} 8 /> 9 ); 10}
Benefits of controlled components:
Uncontrolled components rely on the DOM to handle form data:
1function UncontrolledInput() { 2 const inputRef = useRef(); 3 4 function handleSubmit() { 5 console.log("Input value:", inputRef.current.value); 6 } 7 8 return ( 9 <> 10 <input ref={inputRef} defaultValue="Initial value" /> 11 <button onClick={handleSubmit}>Submit</button> 12 </> 13 ); 14}
Benefits of uncontrolled components:
Forms are a fundamental part of React applications, allowing for user interaction and data collection. React's approach to form handling gives you powerful tools to create engaging and responsive user interfaces. Whether you choose controlled or uncontrolled components depends on your specific requirements, but controlled components are generally preferred for most React applications due to their predictability and integration with React's state management.