You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+40-25Lines changed: 40 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1114,43 +1114,52 @@ class ParentComponent extends React.Component {
1114
1114
1115
1115
21. ### What are controlled components?
1116
1116
1117
-
A componentthat 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.
1118
1118
1119
1119
The controlled components will be implemented using the below steps,
1120
1120
1121
1121
1. Initialize the state using `useState` hooks in function components or inside constructor for class components.
1122
1122
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
1125
1125
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.
1127
1127
1128
-
```javascript
1129
-
import React, { useState } from "react";
1128
+
For example, the name input field updates the username using `handleChange` event handler as below,
In these components, DOM does not hold the actual data instead React does.
1151
+
1152
+
**Benefits:**
1150
1153
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.
1152
1157
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.
1154
1163
1155
1164
The uncontrolled components will be implemented using the below steps,
1156
1165
@@ -1182,6 +1191,12 @@ class ParentComponent extends React.Component {
1182
1191
);
1183
1192
}
1184
1193
```
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).
1185
1200
1186
1201
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.
1187
1202
@@ -1218,7 +1233,7 @@ class ParentComponent extends React.Component {
1218
1233
</p>
1219
1234
</details>
1220
1235
1221
-
**[⬆ Back to Top](#table-of-contents)**
1236
+
**[⬆ Back to Top](#table-of-contents)**
1222
1237
1223
1238
23. ### What is the difference between createElement and cloneElement?
0 commit comments