Skip to content

Commit c473129

Browse files
committed
update context and hook reducer exercises
1 parent 5cdee27 commit c473129

File tree

3 files changed

+80
-16
lines changed

3 files changed

+80
-16
lines changed

src/components/patterns/Context/Page.jsx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,32 @@ const Page = () => (
2222
<Example />
2323
<hr />
2424
<Exercise />
25-
<h3>Bonus Exercise</h3>
26-
<p>
27-
<input type="checkbox" />
28-
b.1. In{" "}
29-
<code>
30-
src/components/patterns/Context/exercise/GraphQLProvider.jsx
31-
</code>{" "}
32-
we are using <code>const memoizedHashGql = memoize(hashGql);</code>.
33-
Should we use useMemo instead? Why?
34-
</p>
25+
26+
<h3>Bonus Exercise 1</h3>
27+
<p>Now that you know how the React Context works:</p>
28+
<ul>
29+
<li>
30+
<input type="checkbox" /> Would use the React Context for the form in
31+
the previous React Reducer Exercise? What are the pros and cons?
32+
</li>
33+
<li>
34+
<input type="checkbox" /> If you use the Context for the form, would you
35+
pass the value of the field and other props to the Field component using
36+
context or props?
37+
</li>
38+
<li>
39+
<input type="checkbox" /> If you pass the value of the "input" and the
40+
other required props to the Field component using context, does it still
41+
make sense to use the React.memo HoC in the Field component?
42+
</li>
43+
</ul>
44+
45+
<h3>Bonus Exercise 2</h3>
3546
<p>
36-
<input type="checkbox" />
37-
b.2. In our current implementation, although there is a cache (data key in
38-
our reducer) for each pair query & variables, we can only send 1 query at
39-
a time. How would you make it possible to send requests concurrently?
47+
<input type="checkbox" /> In our current implementation, although there is
48+
a cache (data key in our reducer) for each pair query & variables, we can
49+
only send 1 query at a time. How would you make it possible to send
50+
requests concurrently?
4051
</p>
4152
</React.Fragment>
4253
);

src/components/patterns/Context/exercise/index.jsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const Root = () => {
6464
your useQuery. You can create a handy useClient custom hook like we did
6565
in the example{" "}
6666
<code>
67-
src/components/patterns/Context/example/modal.jsx -> useModal function{" "}
67+
src/components/patterns/Context/example/modal.jsx : useModal function{" "}
6868
</code>
6969
</p>
7070

@@ -97,6 +97,15 @@ const Root = () => {
9797
wait 🤔... does it make any difference in our use case? Why? Discuss
9898
with your peers.
9999
</p>
100+
<h4>Tasks part 3:</h4>
101+
<p>
102+
<input type="checkbox" /> 3. In{" "}
103+
<code>
104+
src/components/patterns/Context/exercise/GraphQLProvider.jsx
105+
</code>{" "}
106+
we are using <code>const memoizedHashGql = memoize(hashGql);</code>.
107+
Should we use useMemo instead? Why?
108+
</p>
100109
</div>
101110
);
102111
};

src/components/patterns/HookReducer/Page.jsx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,51 @@ const Page = () => (
8888
</li>
8989
</ul>
9090

91-
<h3>Bonus exercise</h3>
91+
<h3>Exercise part 3</h3>
92+
<p>
93+
By default we are displaying the error message to the user even if the
94+
user did not use the form. That's not a great user experience. To improve
95+
that we are going to add another state in our form to identify which
96+
fields are `dirty`.
97+
</p>
98+
<p>
99+
A field is dirty when the value of the field is not equal to the initial
100+
value, false if the values are equal.
101+
</p>
102+
103+
<h3>Bonus exercise part 1</h3>
104+
<p>
105+
We are going to add some state to our form to know when the form is being
106+
submitted.
107+
</p>
108+
<p>
109+
If the form is being submitted then we'll display the text "submitting"
110+
instead of "submit" in the submit button.
111+
</p>
112+
113+
<h3>Bonus exercise part 2</h3>
114+
<p>
115+
Use the{" "}
116+
<a
117+
href="https://reactjs.org/blog/2018/09/10/introducing-the-react-profiler.htm"
118+
target="_blank"
119+
>
120+
React Profiler
121+
</a>{" "}
122+
in the React Dev Tools to record what happens when you type in the user
123+
id. Is the password being rendered as well? Why?
124+
</p>
125+
<p>
126+
To avoid unnecessary renders you can create another component called
127+
"Field" and use{" "}
128+
<a
129+
href="https://reactjs.org/docs/react-api.html#reactmemo"
130+
target="_blank"
131+
>
132+
React.memo
133+
</a>
134+
.
135+
</p>
92136
</React.Fragment>
93137
);
94138

0 commit comments

Comments
 (0)