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
3. We don’t explain everything you need to know before the exercise:
16
+
- Learn by doing (and teaching ;)
17
+
- The exercise is meant to help you build a mental model
4
18
5
19
## Prerequisites
6
20
7
21
You need to be comfortable writing JavaScript and HTML to do this exercise. The exercise uses the following ES6 & ES5 features:
8
22
9
23
- Module system ([import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import)/ [export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export))
-[Array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) and [Array.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
13
26
@@ -26,49 +39,53 @@ npm install
26
39
npm start
27
40
```
28
41
29
-
## Exercise
42
+
## 🥑 Before we start the exercise
30
43
31
-
Before you start, there are two types of components [Function Components and Class Components](https://reactjs.org/docs/components-and-props.html#function-and-class-components).
44
+
Before you start, we are going to use the [useState hook](https://reactjs.org/docs/hooks-state.html) in this exercise.
32
45
33
46
```javascript
34
-
functionWelcome(props) {
35
-
return<h1>Hello, {props.name}</h1>;
47
+
importReact, { useState } from'react';
48
+
49
+
functionExample() {
50
+
const [count, setCount] =useState(0);
51
+
52
+
return (
53
+
<div>
54
+
<p>You clicked {count} times</p>
55
+
<button onClick={() =>setCount(count +1)}>
56
+
Click me
57
+
</button>
58
+
</div>
59
+
);
36
60
}
37
61
```
38
62
39
-
```javascript
40
-
classWelcomeextendsReact.Component {
41
-
render() {
42
-
return<h1>Hello, {this.props.name}</h1>;
43
-
}
44
-
}
45
-
```
46
-
47
-
Try to use a Function component if the component doesn't have state, you'll need to refactor code a few times during the next exercise 😁
63
+
The goal of this exercise is to understand what's state and how to reason about it. The goal of this exercise is not to learn how the useState hook works. How we store the state is an implementation detail. The principles you'll learn in this exercise can be applied to classes & this.setState, hooks, and some other state management libraries.
48
64
49
-
### Tasks
65
+
##🤸♀️ Exercise
50
66
51
67
-[ ]1. Refactor the “about” and “footer” sections by creating a function component for each.
52
68
Make sure everything works.
53
69
54
70
-[ ]2. Refactor the navbar by creating a Function Component.
55
-
Pass the dependencies (`this.toggleMenu` in this case) via props.
71
+
Pass the dependencies (`toggleMenu` in this case) via props.
56
72
Make sure everything works by clicking on the "Training" button at the top right of the screen.
57
73
58
74
-[ ]3. Refactor the books section by creating a function component and pass the dependencies via props.
59
75
Make sure everything works.
60
76
61
77
-[ ]4. Is there any state in app that should be in the Books component?
62
-
Refactor `<Books>` if appropriate. Should `<Books>` be a Function Component or a Class Component now?
78
+
Refactor `<Books>` if appropriate.
63
79
64
80
-[ ]5. Break `<Books>` down into `<BookList>` and `<BookFilter>`
65
81
66
-
-[ ]6. What do you think it would make sense to componentize next?
67
-
Are there any parts on that view that you can reuse? Try to explain to a mentor what you want to refactor before you code 😁
82
+
## 🏋️♀️ Bonus exercise
83
+
- What do you think it would make sense to componentize next? Are there any parts on that view that you can reuse? Hint, replace the `<a>` with your `<Link>` component. Who are the children of the `Link` component? The `Link` component should receive a prop called `to` that becomes the href of the `<a href={to} ...`. Try to use the prop called `children` for the text displayed inside the anchor.
84
+
- Can we move the `isMenuOpen` state inside the menu? Does it conflict with the idea of "lifting the state up".
85
+
- If you look at the [React Profiler](https://reactjs.org/blog/2018/09/10/introducing-the-react-profiler.html) when you open and close the menu, is the whole app being rendered? If so, how can we avoid that and still lift the state up?
68
86
69
87
## Articles and links
70
88
71
-
72
89
-[Lecture: Introduction to Thinking in React](https://reactgraphql.academy/react/introduction-to-thinking-in-react/)
73
90
-[A Beginner’s Guide to React](https://medium.com/leanjs/introduction-to-react-3000e9cbcd26)
74
91
-[Introduction to JSX](https://reactjs.org/docs/introducing-jsx.html)
0 commit comments