Navigating React: Understanding Controlled and Uncontrolled Components

Eftee Codes
2 min readDec 17, 2023

--

Let’s delve into the captivating realm of React components, with a specific focus on both controlled and uncontrolled components. Whether you’re a seasoned React enthusiast or just embarking on your journey, grasping these fundamental concepts is imperative for constructing resilient and effective user interfaces.

What are Controlled Components?

Controlled components in React are components whose state is fully controlled by the application’s state. In other words, their behavior is determined by the state passed down to them as props.

import React, { useState } from 'react';

const ControlledInput = () => {
const [inputValue, setInputValue] = useState('');

const handleChange = (e) => {
setInputValue(e.target.value);
};

return (
<div>
<label>Controlled Input:</label>
<input
type="text"
value={inputValue}
onChange={handleChange}
/>
<p>Value: {inputValue}</p>
</div>
);
};

export default ControlledInput;

In this example, the inputValue is controlled by the React state. The input field's value is set to the state (value={inputValue}), and any changes trigger the handleChange function, updating the state accordingly.

Now, let’s explore Uncontrolled Components:

Uncontrolled components, on the other hand, maintain their own internal state. React does not control their state directly; instead, it is managed by the DOM itself. Uncontrolled components are useful in scenarios where you want to integrate React with non-React code or when dealing with controlled components becomes cumbersome.

import React, { useRef } from 'react';

const UncontrolledInput = () => {
const inputRef = useRef(null);

const handleButtonClick = () => {
alert(`Input Value: ${inputRef.current.value}`);
};

return (
<div>
<label>Uncontrolled Input:</label>
<input type="text" ref={inputRef} />
<button onClick={handleButtonClick}>Get Value</button>
</div>
);
};

export default UncontrolledInput;

In this example, the inputRef is used to access the DOM input element directly. The value is obtained without explicitly tracking it through React state.

Comparing Controlled and Uncontrolled Components:

In terms of state management, controlled components entrust React with the responsibility of managing the state, while uncontrolled components rely on the DOM to handle their internal state. Concerning performance, controlled components involve React overseeing every state change, whereas uncontrolled components may offer enhanced performance, particularly in situations involving extensive forms. When it comes to integration, controlled components exhibit superior alignment with React’s declarative paradigm, while uncontrolled components prove valuable when integrating with external non-React libraries or when confronted with intricate DOM interactions.

Conclusion:

Understanding controlled and uncontrolled components in React empowers you to make informed decisions when building your applications. Both approaches have their merits, and choosing the right one depends on the specific requirements of your project.

Feel free to experiment with the provided examples and share your thoughts on when you would prefer one approach over the other! Happy coding!

--

--

Eftee Codes
Eftee Codes

Written by Eftee Codes

Full Stack Developer | Tech Enthusiast | Psychology & Economics Aficionado | Turning Code into Fun Experiences 🎮🧠

No responses yet