Skip to content

Commit efb9974

Browse files
committed
Improved controlled and uncontrolled components
1 parent b762ba6 commit efb9974

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

README.md

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,43 +1114,52 @@ class ParentComponent extends React.Component {
11141114

11151115
21. ### What are controlled components?
11161116

1117-
A component that controls the input elements within the forms on subsequent user input is called **Controlled Component**, i.e, every state mutation will have an associated handler function. That means, the displayed data is always in sync with the state of the component.
1117+
A **controlled component** is a React component that **fully manages the form element's state**(e.g, elements like `<input>`, `<textarea>`, or `<select>`)) using React's internal state mechanism. i.e, The component does not manage its own internal state — instead, React acts as the single source of truth for form data.
11181118

11191119
The controlled components will be implemented using the below steps,
11201120

11211121
1. Initialize the state using `useState` hooks in function components or inside constructor for class components.
11221122
2. Set the value of the form element to the respective state variable.
1123-
3. Create an event handler to handle the user input changes through useState updater function or setState from class component.
1124-
4. Attach the above event handler to form elements change or click events
1123+
3. Create an event handler(`onChange`) to handle the user input changes through `useState`'s updater function or `setState` from class component.
1124+
4. Attach the above event handler to form element's change or click events
11251125

1126-
For example, the name input field updates the user name using `handleChange` event handler as below,
1126+
**Note:** React re-renders the component every time the input value changes.
11271127

1128-
```javascript
1129-
import React, { useState } from "react";
1128+
For example, the name input field updates the username using `handleChange` event handler as below,
11301129

1131-
function UserProfile() {
1132-
const [username, setUsername] = useState("");
1130+
```javascript
1131+
import React, { useState } from "react";
11331132

1134-
const handleChange = (e) => {
1135-
setUsername(e.target.value);
1136-
};
1133+
function UserProfile() {
1134+
const [username, setUsername] = useState("");
11371135

1138-
return (
1139-
<form>
1140-
<label>
1141-
Name:
1142-
<input type="text" value={username} onChange={handleChange} />
1143-
</label>
1144-
</form>
1145-
);
1146-
}
1147-
```
1136+
const handleChange = (e) => {
1137+
setUsername(e.target.value);
1138+
};
11481139

1149-
**[⬆ Back to Top](#table-of-contents)**
1140+
return (
1141+
<form>
1142+
<label>
1143+
Name:
1144+
<input type="text" value={username} onChange={handleChange} />
1145+
</label>
1146+
</form>
1147+
);
1148+
}
1149+
```
1150+
In these components, DOM does not hold the actual data instead React does.
1151+
1152+
**Benefits:**
11501153
1151-
22. ### What are uncontrolled components?
1154+
* Easy to implement **validation**, **conditional formatting**, or **live feedback**.
1155+
* Full control over form data.
1156+
* Easier to test and debug because the data is centralized in the component’s state.
11521157
1153-
The **Uncontrolled Components** are the ones that store their own state internally, and you query the DOM using a `ref` to find its current value when you need it. This is a bit more like traditional HTML.
1158+
**[⬆ Back to Top](#table-of-contents)**
1159+
1160+
22. ### What are uncontrolled components?
1161+
The **Uncontrolled components** are form elements (like `<input>`, `<textarea>`, or `<select>`) that **manage their own state internally** via the **DOM**, rather than through React state.
1162+
You can query the DOM using a `ref` to find its current value when you need it. This is a bit more like traditional HTML.
11541163
11551164
The uncontrolled components will be implemented using the below steps,
11561165
@@ -1182,6 +1191,12 @@ class ParentComponent extends React.Component {
11821191
);
11831192
}
11841193
```
1194+
**Note:** Here, DOM is in charge of the value. React only accesses the value when needed (via `ref`).
1195+
1196+
**Benefits:**
1197+
* **Less boilerplate** — no need for `useState` and `onChange`.
1198+
* Useful for **quick form setups** or when integrating with **non-React code**.
1199+
* Slightly better **performance** in very large forms (fewer re-renders).
11851200
11861201
In most cases, it's recommend to use controlled components to implement forms. In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself.
11871202
@@ -1218,7 +1233,7 @@ class ParentComponent extends React.Component {
12181233
</p>
12191234
</details>
12201235
1221-
**[⬆ Back to Top](#table-of-contents)**
1236+
**[⬆ Back to Top](#table-of-contents)**
12221237
12231238
23. ### What is the difference between createElement and cloneElement?
12241239

0 commit comments

Comments
 (0)