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: lib/components_guide_web/templates/react_typescript/forms.html.md
+25-11Lines changed: 25 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,24 @@
1
1
## Deciding whether to control a form element
2
2
3
-
- A controlled element lets you have **full control** over its value.
4
-
- An uncontrolled element can let you handle events with less code.
3
+
If you’ve used React for a while, or have read the [React docs](https://reactjs.org/docs/uncontrolled-components.html), you’ve probably come across controller vs uncontrolled components. Here’s a summary of each’s benefits:
5
4
6
-
## Controlled form elements
5
+
- A controlled form element lets you have **full control** over its value.
6
+
- An uncontrolled form element can let you handle events with **less code**.
7
7
8
-
- We store the state for each field using `useState`.
8
+
Instead of thinking about controlled vs uncontrolled components, it can be helpful to think about implicit vs explicit state:
9
+
10
+
- A form element with **explicit state** has its state managed externally.
11
+
- A form element with **implicit state** manages its own state.
12
+
13
+
## Controlled form elements, or explicit state
14
+
15
+
- State is explicit and owned by us.
16
+
- We store the state for each field using `useState` or `useReducer`.
9
17
- We must set the current `value` of the input when rendering.
10
18
- We must listen to when the user changes using `onChange` on each input, and update our state.
11
-
- We can then read our state when the form is submitted.
19
+
- We can then read our local state when say the form is submitted.
20
+
21
+
Here’s an example of a Sign In form with email and password inputs using controlled state:
12
22
13
23
```tsx
14
24
importReact, { useState } from"react";
@@ -52,12 +62,14 @@ function SignInForm() {
52
62
}
53
63
```
54
64
55
-
## Uncontrolled form elements
65
+
## Uncontrolled form elements, or implicit state
56
66
57
-
- We have no state — the input itself holds the state.
58
-
- We could set an initial value using `defaultValue`.
59
-
- We don’t have to listen to any change events.
60
-
- We can then read from the form using the DOM when it is submitted.
67
+
- State is implicit, tucked away inside the form control element and managed by React for us.
68
+
- We can set an initial value using `defaultValue`.
69
+
- We don’t _have_ to listen to any change events.
70
+
- We can then read using the DOM when the form is submitted.
71
+
72
+
Here’s an example of a Sign In form with email and password inputs using uncontrolled state, and reading the inputted values using [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData):
0 commit comments