Skip to content

Commit f6fdc21

Browse files
committed
part 7b
1 parent d4f4576 commit f6fdc21

File tree

2 files changed

+84
-105
lines changed

2 files changed

+84
-105
lines changed

src/content/7/en/part7b.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ lang: en
99

1010
### Hooks
1111

12-
React offers 15 different [built-in hooks](https://react.dev/reference/react), of which the most popular ones are the [useState](https://react.dev/reference/react/useState) and [useEffect](https://react.dev/reference/react/useEffect) hooks that we have already been using extensively.
12+
React offers 15 different [built-in hooks](https://react.dev/reference/react/hooks), of which the most popular ones are the [useState](https://react.dev/reference/react/useState) and [useEffect](https://react.dev/reference/react/useEffect) hooks that we have already been using extensively.
1313

1414
In [part 5](/en/part5/props_children_and_proptypes#references-to-components-with-ref) we used the [useImperativeHandle](https://react.dev/reference/react/useImperativeHandle) hook which allows components to provide their functions to other components. In [part 6](/en/part6/react_query_use_reducer_and_the_context) we used [useReducer](https://react.dev/reference/react/useReducer) and [useContext](https://react.dev/reference/react/useContext) to implement a Redux like state management.
1515

1616
Within the last couple of years, many React libraries have begun to offer hook-based APIs. [In part 6](/en/part6/flux_architecture_and_redux) we used the [useSelector](https://react-redux.js.org/api/hooks#useselector) and [useDispatch](https://react-redux.js.org/api/hooks#usedispatch) hooks from the react-redux library to share our redux-store and dispatch function to our components.
1717

1818
The [React Router's](https://reactrouter.com/en/main/start/tutorial) API we introduced in the [previous part](/en/part7/react_router) is also partially hook-based. Its hooks can be used to access URL parameters and the <i>navigation</i> object, which allows for manipulating the browser URL programmatically.
1919

20-
As mentioned in [part 1](/en/part1/a_more_complex_state_debugging_react_apps#rules-of-hooks), hooks are not normal functions, and when using those we have to adhere to certain [rules or limitations](https://legacy.reactjs.org/docs/hooks-rules.html). Let's recap the rules of using hooks, copied verbatim from the official React documentation:
20+
As mentioned in [part 1](/en/part1/a_more_complex_state_debugging_react_apps#rules-of-hooks), hooks are not normal functions, and when using those we have to adhere to certain [rules or limitations](https://react.dev/warnings/invalid-hook-call-warning#breaking-rules-of-hooks). Let's recap the rules of using hooks, copied verbatim from the official React documentation:
2121

2222
**Don’t call Hooks inside loops, conditions, or nested functions.** Instead, always use Hooks at the top level of your React function.
2323

24-
**Don’t call Hooks from regular JavaScript functions.** Instead, you can:
24+
**You can only call Hooks while React is rendering a function component:**
2525

26-
- Call Hooks from React function components.
27-
- Call Hooks from custom Hooks
26+
- Call them at the top level in the body of a function component.
27+
- Call them at the top level in the body of a custom Hook.
2828

2929
There's an existing [ESlint plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) that can be used to verify that the application uses hooks correctly:
3030

@@ -141,7 +141,7 @@ const App = () => {
141141

142142
The application creates <i>two</i> completely separate counters. The first one is assigned to the variable _left_ and the other to the variable _right_.
143143

144-
Dealing with forms in React is somewhat tricky. The following application presents the user with a form that requests the user to input their name, birthday, and height:
144+
Dealing with forms in React is somewhat tricky. The following application presents the user with a form that requires him to input their name, birthday, and height:
145145

146146
```js
147147
const App = () => {
@@ -201,7 +201,7 @@ const useField = (type) => {
201201
}
202202
```
203203

204-
The hook function receives the type of the input field as a parameter. The function returns all of the attributes required by the <i>input</i>: its type, value and the onChange handler.
204+
The hook function receives the type of the input field as a parameter. It returns all of the attributes required by the <i>input</i>: its type, value and the onChange handler.
205205

206206
The hook can be used in the following way:
207207

@@ -227,7 +227,7 @@ const App = () => {
227227

228228
### Spread attributes
229229

230-
We could simplify things a bit further. Since the _name_ object has exactly all of the attributes that the <i>input</i> element expects to receive as props, we can pass the props to the element using the [spread syntax](https://react.dev/learn/updating-objects-in-state#copying-objects-with-the-spread-syntax) in the following way:
230+
We could simplify things a bit further. Since the _name_ object has exactly all of the attributes that the <i>input</i> element expects to receive as props, we can pass the props to the element using the [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) in the following way:
231231

232232
```js
233233
<input {...name} />
@@ -276,7 +276,7 @@ const App = () => {
276276

277277
Dealing with forms is greatly simplified when the unpleasant nitty-gritty details related to synchronizing the state of the form are encapsulated inside our custom hook.
278278

279-
Custom hooks are not only a tool for reuse; they also provide a better way for dividing our code into smaller modular parts.
279+
Custom hooks are not only a tool for reusing code; they also provide a better way for dividing it into smaller modular parts.
280280

281281
### More about hooks
282282

@@ -292,9 +292,9 @@ The internet is starting to fill up with more and more helpful material related
292292

293293
### Exercises 7.4.-7.8.
294294

295-
We'll continue with the app from [exercises](/en/part7/react_router#exercises-7-1-7-3) of the chapter [react router](/en/part7/react_router).
295+
We'll continue with the app from the [exercises](/en/part7/react_router#exercises-7-1-7-3) of the [react router](/en/part7/react_router) chapter.
296296

297-
#### 7.4: anecdotes and hooks step1
297+
#### 7.4: Anecdotes and Hooks step 1
298298

299299
Simplify the anecdote creation form of your application with the _useField_ custom hook we defined earlier.
300300

@@ -337,7 +337,7 @@ const App = () => {
337337
}
338338
```
339339

340-
#### 7.5: anecdotes and hooks step2
340+
#### 7.5: Anecdotes and Hooks step 2
341341

342342
Add a button to the form that you can use to clear all the input fields:
343343

@@ -351,11 +351,11 @@ Depending on your solution, you may see the following warning in your console:
351351

352352
We will return to this warning in the next exercise.
353353

354-
#### 7.6: anecdotes and hooks step3
354+
#### 7.6: Anecdotes and Hooks step 3
355355

356356
If your solution did not cause a warning to appear in the console, you have already finished this exercise.
357357

358-
If you see the warning in the console, make the necessary changes to get rid of the _Invalid value for prop \`reset\` on \<input\> tag_ console warning.
358+
If you see the _Invalid value for prop \`reset\` on \<input\> tag_ warning in the console, make the necessary changes to get rid of it.
359359

360360
The reason for this warning is that after making the changes to your application, the following expression:
361361

@@ -386,15 +386,15 @@ One simple fix would be to not use the spread syntax and write all of the forms
386386
/>
387387
```
388388

389-
If we were to do this, we would lose much of the benefit provided by the <i>useField</i> hook. Instead, come up with a solution that fixes the issue, but is still easy to use with spread syntax.
389+
If we were to do this, we would lose much of the benefit provided by the <i>useField</i> hook. Instead, come up with a solution that fixes the issue, but is still easy to use with the spread syntax.
390390

391-
#### 7.7: country hook
391+
#### 7.7: Country hook
392392

393393
Let's return to exercises [2.18-2.20](/en/part2/adding_styles_to_react_app#exercises-2-18-2-20).
394394

395395
Use the code from <https://github.com/fullstack-hy2020/country-hook> as your starting point.
396396

397-
The application can be used to search for a country's details from the service in <https://studies.cs.helsinki.fi/restcountries/>. If a country is found, the details of the country are displayed:
397+
The application can be used to search for a country's details from the service in <https://studies.cs.helsinki.fi/restcountries/>. If a country is found, its details are displayed:
398398

399399
![browser displaying country details](../../images/7/69ea.png)
400400

@@ -408,7 +408,7 @@ Use the API endpoint [name](https://studies.cs.helsinki.fi/restcountries/) to fe
408408

409409
Note that in this exercise it is essential to use useEffect's [second parameter](https://react.dev/reference/react/useEffect#parameters) array to control when the effect function is executed. See the course [part 2](/en/part2/adding_styles_to_react_app#couple-of-important-remarks) for more info how the second parameter could be used.
410410

411-
#### 7.8: ultimate hooks
411+
#### 7.8: Ultimate Hooks
412412

413413
The code of the application responsible for communicating with the backend of the note application of the previous parts looks like this:
414414

@@ -448,7 +448,7 @@ We notice that the code is in no way specific to the fact that our application d
448448

449449
Extract the code for communicating with the backend into its own _useResource_ hook. It is sufficient to implement fetching all resources and creating a new resource.
450450

451-
You can do the exercise for the project found in the <https://github.com/fullstack-hy2020/ultimate-hooks> repository. The <i>App</i> component for the project is the following:
451+
You can do the exercise in the project found in the <https://github.com/fullstack-hy2020/ultimate-hooks> repository. The <i>App</i> component for the project is the following:
452452

453453
```js
454454
const App = () => {

0 commit comments

Comments
 (0)