What are Controlled Elements in React? Beginners Guide

Code Your Thoughts
3 min readNov 21, 2023

--

Contents:

  1. What are controlled elements ?
  2. How to create a controlled element ?
    a. Initialize state
    b. Handle changes
    c. Bind state to element
  3. Advantages of controlled elements
  4. Conclusion

1. What are Controlled Elements in React ?

A controlled element is an input form element whose value is controlled by the React component’s state.
Unlike uncontrolled elements, where the DOM itself manages the state of the input, controlled elements allow React to maintain control over the input’s value.

2. How to create a controlled element ?

To implement controlled elements in React, follow these steps:

Step 1: Initialize State

First, create a state variable to store the value of the controlled element. In React, you can use the useState hook to initialize state.
Here's an example with a controlled input:

import React, { useState } from 'react';

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

// Rest of your component...
}

In this example, inputValue is the state variable that will hold the value of the controlled input.

Step 2: Handle Changes

Next, implement a function to handle changes to the controlled element’s value.
This function will update the state whenever the the input value changes. For the controlled input example:

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

const handleInputChange = (event) => {
setInputValue(event.target.value);
};

// Rest of your component...
}

Here, handleInputChange is the function that updates the state (inputValue) with the new value whenever the input changes.

Step 3: Bind State to Element

Finally, bind the state variable and the handler function to the controlled element.

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

const handleInputChange = (event) => {
setInputValue(event.target.value);
};

return (
<input
type="text"
value={inputValue}
onChange={handleInputChange}
/>
);
}

In the above code, the value attribute of the input is bound to the inputValue state, and the onChange event is handled by the handleInputChange function.
This creates a controlled input where React manages the state of the input element.

By following these steps, we establish a controlled element in React, gaining better control and predictability over our component’s state.

3. Advantages of Controlled Elements

Controlled elements present several benefits compared to uncontrolled elements:

  1. Predictable State:
    Controlled elements guarantee that the input is always in a known and predictable state. This simplifies the management of the input’s value as required
  2. Dynamic Updates:
    The ability to control the input value facilitates dynamic updates and validations. Implementing features like real-time validation, conditional rendering, or providing instant feedback to users becomes effortless with controlled elements.
  3. Testing:
    With controlled elements in React, testing becomes easier because React is in charge of the input’s state. It’s like having a clear rulebook — you always know how the input should behave.

4. Conclusion

In summary, controlled elements in React play a crucial role in managing form inputs and state. By tapping into React’s state management capabilities, developers can build more interactive, adaptable, and easy-to-maintain user interfaces. Mastering controlled elements is an essential skill for React developers, enhancing the overall efficiency and reliability of their applications.

Thank you 🙏 for taking the time to read this article. I trust you found it beneficial. If you have any feedback or questions 🤔, please feel free to share them. For more insightful blog posts, consider following me. Your support is greatly appreciated!

I would greatly appreciate your thoughts and feedback on this article. Please feel free to leave your comments below. I am eager to hear your perspectives, suggestions, or any additional insights you may have💬🤔🙌

--

--

Code Your Thoughts

Exploring tech, programming, and coding. Unraveling trends, sharing insights, and empowering enthusiasts. https://linkedin.com/in/raj-ritik