Skip to content

Commit 135a4ec

Browse files
committed
Improve React forms article
1 parent ecb0cac commit 135a4ec

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

lib/components_guide_web/templates/react_typescript/forms.html.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
## Deciding whether to control a form element
22

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:
54

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**.
77

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`.
917
- We must set the current `value` of the input when rendering.
1018
- 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:
1222

1323
```tsx
1424
import React, { useState } from "react";
@@ -52,12 +62,14 @@ function SignInForm() {
5262
}
5363
```
5464

55-
## Uncontrolled form elements
65+
## Uncontrolled form elements, or implicit state
5666

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):
6173

6274
```tsx
6375
import React from "react";
@@ -72,7 +84,9 @@ function SignInForm() {
7284
const data = new FormData(form);
7385
const email = data.get('email');
7486
const password = data.get('password');
75-
// Could validate here.
87+
88+
// We could validate at this point if we wanted.
89+
7690
authService.signIn({ email, password });
7791
}}
7892
>

0 commit comments

Comments
 (0)