Skip to main content
PHP Blog

Back to all posts

How to Create A Controlled Component In React?

Published on
5 min read
How to Create A Controlled Component In React? image

Best Books on React Components to Buy in October 2025

1 The Road to React: Your journey to master plain yet pragmatic React.js

The Road to React: Your journey to master plain yet pragmatic React.js

BUY & SAVE
$29.99
The Road to React: Your journey to master plain yet pragmatic React.js
2 React: The Comprehensive Guide to Mastering React.js with Hands-on Examples, Expert Tips, and Everything You Need to Build Dynamic, Scalable User Interfaces (Rheinwerk Computing)

React: The Comprehensive Guide to Mastering React.js with Hands-on Examples, Expert Tips, and Everything You Need to Build Dynamic, Scalable User Interfaces (Rheinwerk Computing)

BUY & SAVE
$41.31 $59.95
Save 31%
React: The Comprehensive Guide to Mastering React.js with Hands-on Examples, Expert Tips, and Everything You Need to Build Dynamic, Scalable User Interfaces (Rheinwerk Computing)
3 Learn React with TypeScript: A beginner's guide to building real-world, production-ready web apps with React 19 and TypeScript

Learn React with TypeScript: A beginner's guide to building real-world, production-ready web apps with React 19 and TypeScript

BUY & SAVE
$40.49 $49.99
Save 19%
Learn React with TypeScript: A beginner's guide to building real-world, production-ready web apps with React 19 and TypeScript
4 Learning React: Modern Patterns for Developing React Apps

Learning React: Modern Patterns for Developing React Apps

BUY & SAVE
$36.49 $65.99
Save 45%
Learning React: Modern Patterns for Developing React Apps
5 Modern Full-Stack React Projects: Build, maintain, and deploy modern web apps using MongoDB, Express, React, and Node.js

Modern Full-Stack React Projects: Build, maintain, and deploy modern web apps using MongoDB, Express, React, and Node.js

BUY & SAVE
$20.60 $44.99
Save 54%
Modern Full-Stack React Projects: Build, maintain, and deploy modern web apps using MongoDB, Express, React, and Node.js
6 React and React Native: Build cross-platform JavaScript and TypeScript apps for the web, desktop, and mobile

React and React Native: Build cross-platform JavaScript and TypeScript apps for the web, desktop, and mobile

BUY & SAVE
$26.84 $43.99
Save 39%
React and React Native: Build cross-platform JavaScript and TypeScript apps for the web, desktop, and mobile
7 Fluent React: Build Fast, Performant, and Intuitive Web Applications

Fluent React: Build Fast, Performant, and Intuitive Web Applications

BUY & SAVE
$40.27 $65.99
Save 39%
Fluent React: Build Fast, Performant, and Intuitive Web Applications
8 Advanced React: Deep dives, investigations, performance patterns and techniques

Advanced React: Deep dives, investigations, performance patterns and techniques

BUY & SAVE
$42.00
Advanced React: Deep dives, investigations, performance patterns and techniques
9 React in Depth

React in Depth

BUY & SAVE
$44.95 $59.99
Save 25%
React in Depth
10 Learn React Hooks: Unlock scalable state, performance, and clean code with Hooks, Context, Suspense, and Form Actions

Learn React Hooks: Unlock scalable state, performance, and clean code with Hooks, Context, Suspense, and Form Actions

BUY & SAVE
$35.99 $41.99
Save 14%
Learn React Hooks: Unlock scalable state, performance, and clean code with Hooks, Context, Suspense, and Form Actions
+
ONE MORE?

In React, a controlled component is a component where the value of the input field is controlled by the state of the component. This means that the component's value is always updated to reflect the current state of the component.

To create a controlled component in React, you need to follow these steps:

  1. Define a state to hold the value of the input field.
  2. Initialize the state with an initial value.
  3. Create an input element and set its value attribute to the value stored in the state.
  4. Add an event handler to update the state whenever the value of the input field changes.
  5. Use the value stored in the state to control the input field.

By following these steps, you can create a controlled component in React that allows you to easily manage and manipulate the value of the input field.

What are the key concepts to understand when working with controlled components in React?

  1. Controlled vs Uncontrolled Components: In React, controlled components are those where the value of the form element is controlled by React state and any changes to the value are controlled by React event handlers. Uncontrolled components, on the other hand, store their own state internally and are not controlled by React.
  2. State Management: When working with controlled components, it is important to manage the state of the form elements in the React component's state. This allows for React to have full control over the form elements and their values.
  3. onChange Event Handler: To ensure that the state of the controlled component is updated whenever a user interacts with the form element, the onChange event handler should be used to update the state with the new value. This event handler should be attached to the form element in question.
  4. Value Binding: The value of the form element should be bound to the state in the render method of the component. This ensures that the form element reflects the current value stored in the React component's state.
  5. Controlled Input Elements: When working with controlled input elements such as text inputs, checkboxes, radio buttons, and select elements, it is essential to bind the value of the form element to the state and update it accordingly using the onChange event handler.
  6. Form Submission: When working with controlled components in a form, the form submission should be handled using React's onSubmit event handler. This ensures that all form data is captured and processed by React before being submitted.

Overall, understanding how controlled components work in React and how to manage their state effectively is crucial for building interactive and dynamic forms in React applications.

What is a controlled component in React?

A controlled component in React is a component that renders form elements and controls their behavior by maintaining the input value in the component's state. This means that the component does not rely on the DOM to store or update the input value, but instead relies on React to update the component state and trigger re-renders when the value changes.

By using controlled components, developers can have more control over form elements and easily manipulate their values and behavior. Additionally, controlled components allow for easier validation, error handling, and synchronization with other components in the application.

What is the best way to structure controlled components in a React application?

One commonly recommended way to structure controlled components in a React application is to follow the container/presentation component pattern. This pattern involves separating the logic for managing state (in container components) from the rendering and display of the component (in presentation components).

In this pattern, container components are responsible for handling state changes and passing down props to presentation components. Presentation components are dumb components that simply render UI based on the props they receive.

Here's an example of how you could structure a controlled component using this pattern:

  1. Create a container component that manages the state of the controlled component:

import React, { useState } from 'react'; import ControlledComponent from './ControlledComponent';

const ControlledComponentContainer = () => { const [value, setValue] = useState('');

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

return ( ); };

export default ControlledComponentContainer;

  1. Create a presentation component that simply renders UI based on the props it receives:

import React from 'react';

const ControlledComponent = ({ value, onChange }) => { return ( ); };

export default ControlledComponent;

By following this pattern, your codebase will be easier to manage and debug, as the logic for managing state is separated from the rendering of the component. Additionally, this pattern promotes reusability and maintainability, as it allows you to easily swap out presentation components with different implementations while keeping the state management logic intact.

How to refactor uncontrolled components to controlled components in React?

To refactor uncontrolled components to controlled components in React, follow these steps:

  1. Create a new state variable in your component's state to store the value of the input element.
  2. Update the existing input element to use the state variable as the value prop and an onChange event handler to update the state value when the user types in the input.
  3. Modify any functions or event handlers that rely on the input value to reference the state variable instead of the DOM directly.
  4. Remove any defaultValue prop from the input element that may be setting an initial value based on props.
  5. Test the component to ensure that it functions correctly as a controlled component, with the input value being stored and updated in state.

By following these steps, you can refactor an uncontrolled component to a controlled component in React, allowing for more predictability and control over the component's behavior.