@@ -5,36 +5,84 @@ import Exercise from "./exercise";
5
5
const Page = ( ) => (
6
6
< React . Fragment >
7
7
< h2 > Hook Reducer</ h2 >
8
+ < p >
9
+ From the{ " " }
10
+ < a
11
+ href = "https://reactjs.org/docs/hooks-reference.html#usereducer"
12
+ target = "_blank"
13
+ >
14
+ React docs
15
+ </ a >
16
+ </ p >
17
+ < blockquote >
18
+ React.useReducer is usually preferable to useState when you have complex
19
+ state logic that involves multiple sub-values or when the next state
20
+ depends on the previous one.{ " " }
21
+ < em > (We'll practice this in this exercise)</ em >
22
+ </ blockquote >
23
+ < blockquote >
24
+ React.useReducer also lets you optimize performance for components that
25
+ trigger deep updates because you can pass dispatch down instead of
26
+ callbacks.{ " " }
27
+ < em > (We'll practice this in the next exercise about Context)</ em >
28
+ </ blockquote >
8
29
< h3 > Example</ h3 >
30
+ < p > Basic login form</ p >
9
31
< Example />
32
+ < hr />
33
+ < h3 > 🥑 Before the exercise 🏋️♀️</ h3 >
34
+ < p >
35
+ The code of the example is very similar to the code of the exercise. The
36
+ example contains explanations 👩🏫, the exercise contains tasks 🚧 and hints
37
+ 🕵️♀️. If there are things that you don't understand about the code it's
38
+ better to look at the example. If there are things that are not clear
39
+ about what needs to done in the exercise after checking the tasks, let me
40
+ know.
41
+ </ p >
10
42
11
43
< h3 > Exercise</ h3 >
44
+
45
+ < h4 >
46
+ 🎯 The goal is to understand how to handle complex state logic in our
47
+ components that involves multiple sub-values
48
+ </ h4 >
49
+
12
50
< p >
13
51
Refactor the LoginForm component in{ " " }
14
52
< code > src/components/patterns/HookReducer/exercise/index.jsx</ code > so it
15
- implements the following features:
16
- </ p >
17
- < ul >
18
- < li >
19
- Implement the SET_ERRORS action in the reducer. It should add an errors
20
- object to the the state using the action.payload
21
- </ li >
22
- < li >
23
- < code > dispatch</ code > a SET_ERRORS action with the errors (output from
24
- the
25
- < code > validate(state.values)</ code > invocation) as payload.
26
- </ li >
27
- < li >
28
- < code > dispatch</ code > the SET_ERRORS action only when the state of the
29
- input fields change. Hint, you need to use the useEffect second
30
- argument.
31
- </ li >
32
- < li >
33
- Bonus, refactor the Hook Reducer so it's in a custom hook. You can call
34
- it useForm.
35
- </ li >
36
- </ ul >
53
+ implements the following:
54
+ </ p >
55
+ < p >
56
+ < input type = "checkbox" /> 1. Handles the SET_ERRORS action in the reducer.
57
+ It should add an errors object to the the state using the action.payload
58
+ </ p >
59
+ < p >
60
+ < input type = "checkbox" /> 2. < code > dispatch</ code > a SET_ERRORS action
61
+ with the errors (output from the
62
+ < code > validate(state.values)</ code > invocation) as payload.
63
+ </ p >
64
+ < p >
65
+ < input type = "checkbox" /> 3.< code > dispatch</ code > the SET_ERRORS action
66
+ only when the state of the input fields change. Hint, you need to use the
67
+ useEffect second argument.
68
+ </ p >
69
+
37
70
< Exercise />
71
+ < hr />
72
+ < h3 > Bonus exercise</ h3 >
73
+ < p >
74
+ < input type = "checkbox" /> Create a custom hook from your Login Form. You
75
+ can call it useForm.
76
+ </ p >
77
+ < p >
78
+ 🕵️♀️ Hints :
79
+ < ul >
80
+ < li > Extract the useReducer outside the Login Form</ li >
81
+ < li >
82
+ Don't think of state only, but also functions that create "props".
83
+ </ li >
84
+ </ ul >
85
+ </ p >
38
86
</ React . Fragment >
39
87
) ;
40
88
0 commit comments