3 Usecases of Uncontrolled Component in React

3 Usecases of Uncontrolled Component in React


React, as a declarative and component-based JavaScript library, encourages developers to manage the state of their applications using a unidirectional data flow and the virtual DOM. However, there are scenarios where a more traditional, imperative, and direct interaction with the DOM is desirable. This is where uncontrolled components come into play.

Understanding Uncontrolled Components

In React, a controlled component is one whose state is controlled by React. The state of the component is managed through the useState hook or this.setState method, making React the "single source of truth" for that component.

On the other hand, an uncontrolled component allows the DOM to directly handle the component's state. React does not control the state, and there's no reliance on React's state management mechanisms. Instead, the values are accessed directly from the DOM when needed.

Use Cases for Uncontrolled Components

1. Integrating with Non-React Code

Uncontrolled components shine when integrating React with non-React libraries or code that directly manipulates the DOM. In scenarios where React needs to peacefully coexist with legacy code or third-party libraries that interact with the DOM, uncontrolled components offer a smooth integration.

import React, { useRef } from 'react';

function NonReactLibraryIntegration() {
  const inputRef = useRef();

  const handleClick = () => {
    // Non-React code can directly access the input value
    alert(`Input Value: ${inputRef.current.value}`);
  };

  return (
    <>
      <input type="text" ref={inputRef} />
      <button onClick={handleClick}>Get Value</button>
    </>
  );
}

export default NonReactLibraryIntegration;

2. Forms with a Large Number of Inputs

In forms with a large number of inputs, managing the state of each controlled component can become cumbersome. Uncontrolled components offer a simpler alternative, allowing you to directly access input values when needed without maintaining React state for each input.

import React, { useRef } from 'react';

function LargeForm() {
  const input1Ref = useRef();
  const input2Ref = useRef();

  const handleSubmit = () => {
    // Access input values directly from refs
    const value1 = input1Ref.current.value;
    const value2 = input2Ref.current.value;
    // ... (access values from other input refs)
  };

  return (
    <>
      <input type="text" ref={input1Ref} />
      <input type="text" ref={input2Ref} />
      {/* ... (more input elements) */}
      <button onClick={handleSubmit}>Submit</button>
    </>
  );
}

export default LargeForm;

3. Third-Party Integrations

When integrating with third-party components or libraries that expect to directly access DOM elements, uncontrolled components provide a straightforward approach. React's controlled components might not align perfectly with libraries that assume direct DOM manipulation.

import React, { useRef, useEffect } from 'react';
import ThirdPartyLibrary from 'third-party-library';

function ThirdPartyIntegration() {
  const containerRef = useRef();

  useEffect(() => {
    // Initialize the third-party library with the container element
    ThirdPartyLibrary.init(containerRef.current);
  }, []);

  return <div ref={containerRef}></div>;
}

export default ThirdPartyIntegration;

Conclusion

While controlled components are the preferred choice in most React applications due to their declarative nature and predictability, uncontrolled components offer a pragmatic solution for specific scenarios. The decision to use uncontrolled components should be made judiciously, taking into consideration the specific requirements and constraints of your application.

In essence, uncontrolled components provide a bridge between React and the traditional, imperative way of handling the DOM, offering flexibility and interoperability where needed. The key is to strike the right balance and choose the approach that best aligns with your application's architecture and goals.

Did you find this article valuable?

Support Aman Singh Tomar by becoming a sponsor. Any amount is appreciated!