Home >Web Front-end >JS Tutorial >Controlled vs Uncontrolled Components in React: Choosing the Right Approach
In React, managing form elements like input fields, checkboxes, and textareas can be done using either controlled components or uncontrolled components. These two approaches offer different methods for handling and updating the form data, and understanding the difference between them is essential for choosing the right method for your React applications.
In controlled components, form elements are controlled by the React component's state. The form data is managed through React's state, and any changes made by the user are reflected in the component's state. The React component acts as the "single source of truth" for the form data.
import React, { useState } from 'react'; const ControlledForm = () => { const [inputValue, setInputValue] = useState(''); const handleChange = (e) => { setInputValue(e.target.value); // Update the state with the user input }; return ( <form> <input type="text" value={inputValue} // Value is controlled by React state onChange={handleChange} // Updates state on user input /> <button type="submit">Submit</button> </form> ); }; export default ControlledForm;
In uncontrolled components, form elements are managed by the DOM itself, rather than React's state. React does not track the form element's value directly. Instead, you use refs (references) to access the DOM elements and retrieve their current values.
import React, { useState } from 'react'; const ControlledForm = () => { const [inputValue, setInputValue] = useState(''); const handleChange = (e) => { setInputValue(e.target.value); // Update the state with the user input }; return ( <form> <input type="text" value={inputValue} // Value is controlled by React state onChange={handleChange} // Updates state on user input /> <button type="submit">Submit</button> </form> ); }; export default ControlledForm;
Feature | Controlled Components | Uncontrolled Components | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Data is managed by React’s state. | Data is managed by the DOM, using refs. | |||||||||||||||||||||
Data Flow | Value is passed as a prop and updated via state. | Value is accessed directly through the DOM. | |||||||||||||||||||||
Form Validation | Easy to validate and control form data in real-time. | Validation is more complex and may require additional handling. | |||||||||||||||||||||
Boilerplate Code | Requires more code to manage state and updates. | Less boilerplate code but requires refs for accessing form data. | |||||||||||||||||||||
Performance | May be slower with a large number of form elements. | Faster for large forms or when you don’t need real-time data updates. | |||||||||||||||||||||
Complexity | Offers more flexibility and control over form behavior. | Simpler and more straightforward for simple forms. |
Controlled components are the preferred choice when:
Uncontrolled components are more suitable when:
Both controlled and uncontrolled components have their place in React applications. Controlled components offer greater control and predictability, making them ideal for most complex forms. However, uncontrolled components can be a simpler and more performance-efficient solution for basic forms or when you don't need real-time control over the data.
Understanding when to use each type of component will help you decide the best approach based on your specific use case.
The above is the detailed content of Controlled vs Uncontrolled Components in React: Choosing the Right Approach. For more information, please follow other related articles on the PHP Chinese website!