Skip to content

Commit 92eaae4

Browse files
committed
add hook version
1 parent 9a7c1ab commit 92eaae4

File tree

3 files changed

+218
-214
lines changed

3 files changed

+218
-214
lines changed

README.md

+39-22
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
## ReactJS Fundamentals - Thinking in React
1+
## React Fundamentals / Thinking in React
22

3-
The goal of this exercise is to learn how to think in React.
3+
The learning objectives of this exercise are:
4+
- Understand the difference between declarative and imperative programming
5+
- Learn how to breaking down your UI into components
6+
- Comprehend what's state, which components should hold it, and when to lift it up.
7+
8+
## Teaching method
9+
10+
1. Collaborative learning environment & pair programming.
11+
- Rooms with small groups
12+
- Work together, discuss, help each other.
13+
2. We try to foster critical thinking.
14+
- ⬆️ Discovery ⬇️ Instruction
15+
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
418

519
## Prerequisites
620

721
You need to be comfortable writing JavaScript and HTML to do this exercise. The exercise uses the following ES6 & ES5 features:
822

923
- 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))
10-
- [Class syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) (extends, constructor)
1124
- [Arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
1225
- [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)
1326

@@ -26,49 +39,53 @@ npm install
2639
npm start
2740
```
2841

29-
## Exercise
42+
## 🥑 Before we start the exercise
3043

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.
3245

3346
```javascript
34-
function Welcome(props) {
35-
return <h1>Hello, {props.name}</h1>;
47+
import React, { useState } from 'react';
48+
49+
function Example() {
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+
);
3660
}
3761
```
3862

39-
```javascript
40-
class Welcome extends React.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.
4864

49-
### Tasks
65+
## 🤸‍♀️ Exercise
5066

5167
- [ ] 1. Refactor the “about” and “footer” sections by creating a function component for each.
5268
Make sure everything works.
5369

5470
- [ ] 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.
5672
Make sure everything works by clicking on the "Training" button at the top right of the screen.
5773

5874
- [ ] 3. Refactor the books section by creating a function component and pass the dependencies via props.
5975
Make sure everything works.
6076

6177
- [ ] 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.
6379

6480
- [ ] 5. Break `<Books>` down into `<BookList>` and `<BookFilter>`
6581

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?
6886

6987
## Articles and links
7088

71-
7289
- [Lecture: Introduction to Thinking in React](https://reactgraphql.academy/react/introduction-to-thinking-in-react/)
7390
- [A Beginner’s Guide to React](https://medium.com/leanjs/introduction-to-react-3000e9cbcd26)
7491
- [Introduction to JSX](https://reactjs.org/docs/introducing-jsx.html)

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "thinking-in-react",
33
"dependencies": {
4-
"react": "^16.2.0",
4+
"react": "^16.13.1",
55
"react-burger-menu": "^2.2.3",
6-
"react-dom": "^16.2.0"
6+
"react-dom": "^16.13.1"
77
},
88
"devDependencies": {
9-
"react-scripts": "^2.1.3",
10-
"prettier": "^1.16.4"
9+
"react-scripts": "^3.4.1",
10+
"prettier": "^2.0.0"
1111
},
1212
"scripts": {
1313
"start": "react-scripts start",

0 commit comments

Comments
 (0)