Skip to content

Commit fd7908c

Browse files
committed
fix: lesson 31
1 parent e770816 commit fd7908c

3 files changed

Lines changed: 36 additions & 10 deletions

File tree

lessons/lesson31/examples/useEffectPage/index.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ function UseEffectPage3({ userId = 2 }) {
4545
// Эффект, зависящий от userId
4646
useEffect(() => {
4747
if (!userId) return;
48+
setIsLoading(true)
4849

4950
console.log("Сработаю при монтировании и изменении userId");
5051

5152
fetch(`https://jsonplaceholder.typicode.com/users/${userId}`)
52-
.then((response) => response.json)
53+
.then((response) => response.json())
5354
.then((json) => setUser(json))
5455
.catch((error) => console.error("Ошибка загрузки:", error))
5556
.finally(() => setIsLoading(false));

lessons/lesson31/examples/useMemoPage/index.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ const todos = [
1515

1616
function UseMemoPage() {
1717
const [filter, setFilter] = useState("all");
18+
const [counter, setCounter] = useState(0);
19+
1820

1921
return (
2022
<div style={{ padding: "20px" }}>
2123
<h1>useMemo</h1>
2224

25+
<button onClick={() => setCounter(c => c + 1)}>
26+
Счётчик: {counter} (не влияет на TodoList!)
27+
</button>
28+
2329
{/* Кнопки фильтров */}
2430
<div style={{ marginBottom: "20px" }}>
2531
<h3>Выберите фильтр:</h3>

lessons/lesson31/lesson.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ description: "Hooks в React: useState, useEffect, useCallback, useMemo"
2525

2626
<!-- v -->
2727

28+
Жизненный цикл компонента в React
29+
30+
```
31+
1. МОНТИРОВАНИЕ - Mounting
32+
├── Конструктор / useState
33+
├── Рендер (первый)
34+
└── useEffect с пустым массивом зависимостей
35+
36+
2. ОБНОВЛЕНИЕ - Updating (может повторяться много раз)
37+
├── Рендер (из-за изменения пропсов/состояния)
38+
└── useEffect (в зависимости от deps)
39+
40+
3. РАЗМОНТИРОВАНИЕ - Unmounting
41+
└── Функция очистки useEffect
42+
```
43+
44+
<!-- v -->
45+
2846
<img src="./images/react-lifecycle.jpg" title="react lifecicles" />
2947

3048
<!-- v -->
@@ -66,25 +84,25 @@ const [state, setState] = useState(initialState);
6684
```jsx
6785
// ❌ ПЛОХО - не работает
6886
function Counter() {
69-
const [age, setAge] = useState(0);
87+
const [count, setCount] = useState(0);
7088

7189
const increment = () => {
72-
setAge(age + 1); // setCount(0 + 1) → setCount(1)
73-
setAge(age + 1); // setCount(0 + 1) → setCount(1)
74-
// Результат: age увеличится только на 1, а не на 2!
90+
setCount(count + 1); // setCount(0 + 1) → setCount(1)
91+
setCount(count + 1); // setCount(0 + 1) → setCount(1)
92+
// Результат: count увеличится только на 1, а не на 2!
7593
};
7694

7795
return <button onClick={increment}>+2</button>;
7896
}
7997

8098
// ✅ ХОРОШО - работает правильно
8199
function Counter() {
82-
const [age, setAge] = useState(0);
100+
const [count, setCount] = useState(0);
83101

84102
const increment = () => {
85-
setAge((prev) => prev + 1); // prev = 0 → 1
86-
setAge((prev) => prev + 1); // prev = 1 → 2
87-
// Результат: age = 2
103+
setCount((prev) => prev + 1); // prev = 0 → 1
104+
setCount((prev) => prev + 1); // prev = 1 → 2
105+
// Результат: count = 2
88106
};
89107

90108
return <button onClick={increment}>+2</button>;
@@ -290,9 +308,10 @@ export default function UseEffectPage({ userId = 2 }) {
290308

291309
useEffect(() => {
292310
if (!userId) return;
311+
setIsLoading(true);
293312

294313
fetch(`https://jsonplaceholder.typicode.com/users/${userId}`)
295-
.then((response) => response.json)
314+
.then((response) => response.json())
296315
.then((json) => setUser(json))
297316
.catch((error) => console.error("Ошибка загрузки:", error))
298317
.finally(() => setIsLoading(false));

0 commit comments

Comments
 (0)