Skip to content

Commit 511be52

Browse files
committed
Add intro to form reducers article
1 parent 93ec55d commit 511be52

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

lib/components_guide_web/templates/react_typescript/form-reducers.html.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Reduce form boilerplate with React reducers
22

3+
In this tutorial we will create a form that validates both when it is submitted and when individual fields are defocused (aka “blurred”).
4+
5+
Often in React this is done using state and event handlers for every field. This is difficult to scale: as you add new fields, you must add another piece of state and yet another event handler.
6+
7+
Fortunately the browser has two features to help us out: `FormData` and event delegation. We can combine these with React’s `useReducer` hook to create a small amount of reusable code without needing a third-party library.
8+
39
## Thinking of validation as a linear process
410

511
We can think of validation as mapping from events to errors.
@@ -12,7 +18,7 @@ When a whole form is submitted, we validate **all** of its fields at once.
1218

1319
It would be great to write the same code to validate either a single field (when it is blurred) or a whole bunch of fields (when their form is submitted).
1420

15-
If we were to sketch out the steps:
21+
If we were to sketch out the steps it would look like:
1622

1723
1. Get the field(s) matching this event.
1824
2. Get the values from the fields.

0 commit comments

Comments
 (0)