1. Controlled Components in React

In a controlled component, React takes full control over the form inputs. The form data is stored in the component’s state, and any user interaction triggers updates to that state. The input value is always synced with the state, meaning React controls both the display and modification of the input’s value.


2. Key Characteristics:

  • State-Driven: The input’s value is determined by the component’s state.
  • Event Handlers: Updates to the form inputs are handled by event listeners (like onChange).
  • Two-Way Binding: Changes in the state update the form value, and user inputs update the state
  • 				
    					import { useState } from 'react';
    
    function ControlledComponent() {
      const [inputValue, setInputValue] = useState('');
    
      const handleInputChange = (e) => {
        setInputValue(e.target.value);
      };
    
      return (
        <div>
          <input type="text" value={inputValue} onChange={handleInputChange} />
          <p>Current Value: {inputValue}</p>
        </div>
      );
    }
    
    				
    			

    In this case, the value of the input is bound to the component’s state (inputValue). Any changes made by the user trigger the onChange event handler to update the state.


    3. Uncontrolled Components in React

    In uncontrolled components, form input values are managed by the DOM itself rather than by React. Instead of maintaining the value in React state, you access the input value directly from the DOM when needed, usually by using refs (React.createRef() or useRef).


    4. Key Characteristics:

  • DOM-Driven: The input’s value is managed by the DOM, not React’s state.
  • Refs for Access: Input values are accessed using references to the DOM element.
  • Simpler Setup: There’s no need for state management for simple use cases
  • 				
    					import { useRef } from 'react';
    
    function UncontrolledComponent() {
      const inputRef = useRef();
    
      const handleSubmit = () => {
        alert(`Input Value: ${inputRef.current.value}`);
      };
    
      return (
        <div>
          <input type="text" ref={inputRef} />
          <button onClick={handleSubmit}>Submit</button>
        </div>
      );
    }
    
    				
    			

    Here, the input value is accessed via the inputRef reference to the DOM element, without React controlling its state.


    4. Differences Between Controlled and Uncontrolled Components

    Data Management:

    • Controlled Components: The data is stored and controlled in the React component’s state.
    • Uncontrolled Components: The data is controlled by the DOM and accessed via refs.

    Complexity:

    • Controlled Components: Require more boilerplate for setting up state and event handlers.
    • Uncontrolled Components: Simpler to implement for basic forms, but harder to maintain for more complex interactions.

    Use Case:

    • Controlled Components: Preferred when you need full control over the form’s behavior, validation, or real-time updates.
    • Uncontrolled Components: Useful for simpler forms where you don’t need real-time validation or control over input.


    4. When to Use Which?

  • Use controlled components when you need to enforce specific input rules, validation, or handle data in real-time (e.g., dynamic form validation).
  • Use uncontrolled components for simpler forms where you don’t need fine-grained control or where performance is a concern (e.g., large forms).

  • 5. Conclusion

    This distinction between controlled and uncontrolled components helps you decide the best approach for managing form data in your React applications, based on the complexity and performance needs of your project.

    ×