-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Slides for 'React Hooks Advanced' lesson
- Loading branch information
Marat Minulin
committed
Dec 30, 2021
1 parent
c591839
commit 7248f99
Showing
9 changed files
with
470 additions
and
157 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
178 changes: 178 additions & 0 deletions
178
presentations/module2/9_ReactHooksAdvanced/ReactHooksAdvanced.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
--- | ||
title: Advanced React Hooks | ||
description: Advanced React Hooks | ||
--- | ||
|
||
# OTUS | ||
|
||
## ReactJS | ||
|
||
<!--v--> | ||
|
||
### Меня хорошо слышно и видно? | ||
|
||
![Не забыл?](https://www.meme-arsenal.com/memes/1bc94af1680d8ec9c2053b076d5f7990.jpg) | ||
|
||
### Не забыл включить запись? | ||
|
||
<!--v--> | ||
|
||
## Advanced React Hooks | ||
|
||
<!--v--> | ||
|
||
## О вебинаре | ||
|
||
<ul> | ||
<li>Дополнительные хуки - useReducer, useDebugValue, useCustomHook</li> | ||
<li>Еще раз про тестирование</li> | ||
<li>Пример хуков из учебного проекта</li> | ||
</ul> | ||
|
||
<!--v--> | ||
|
||
## useReducer | ||
|
||
``` | ||
const [state, dispatch] = useReducer(reducer, initValue, initFunction); | ||
reducer = (prevState, action) => newState; | ||
``` | ||
|
||
<br /> | ||
|
||
<section> | ||
<ol> | ||
<li>Вместо прямого задания нового state, вызывается dispatch. Всё что передано в dispatch -> action в reducer</li> | ||
<li>Для инициализации может принимать третьим аргументом функцию, который преобразует initValue</li> | ||
</ol> | ||
</section> | ||
|
||
<!--v--> | ||
|
||
## useReducer. Пример | ||
|
||
```js | ||
function todosReducer(state, action) { | ||
switch (action.type) { | ||
case "load": | ||
return { | ||
books: [], | ||
loading: true, | ||
}; | ||
// ... остальные действия ... | ||
default: | ||
return state; | ||
} | ||
} | ||
``` | ||
|
||
<!--v--> | ||
|
||
## useReducer. Пример | ||
|
||
```js | ||
function TodosList() { | ||
const [todos, dispatch] = useReducer(todosReducer, []); | ||
|
||
function handleLoadClick() { | ||
dispatch({ type: "load" }); | ||
} | ||
|
||
// ... | ||
} | ||
``` | ||
|
||
<!--v--> | ||
|
||
## useReducer. Упрощенная реализация | ||
|
||
```js | ||
function useReducer(reducer, initialState) { | ||
const [state, setState] = useState(initialState); | ||
|
||
function dispatch(action) { | ||
const nextState = reducer(state, action); | ||
setState(nextState); | ||
} | ||
|
||
return [state, dispatch]; | ||
} | ||
``` | ||
|
||
<!--v--> | ||
|
||
## useReducer. Примеры | ||
|
||
1. Рассмотрим CounterReducer: | ||
- [пример](https://github.com/nickovchinnikov/react-js-tutorial/blob/ce3a3fe6fb565377beb6a06041e49531f47ceb78/lessons/module2/7_ReactHooks/CounterReducer.tsx) использования useReducer | ||
- [тестирование](https://github.com/nickovchinnikov/react-js-tutorial/blob/ce3a3fe6fb565377beb6a06041e49531f47ceb78/lessons/module2/7_ReactHooks/CounterReducer.test.tsx) reducer'а | ||
|
||
2. Реализуем: | ||
<ul> | ||
<li>дополнительные action'ы для CounterReducer</li> | ||
<li>и соответствующие тесты для reducer'а</li> | ||
</ul> | ||
|
||
<!--v--> | ||
|
||
## Вопросы | ||
|
||
<!--v--> | ||
|
||
## useDebugValue | ||
|
||
1. Добавляет некое строковое значение пользовательским Hooks, которое будет отображаться в react-devtools | ||
2. Вторым аргументов (опционально) передается функция трансформации из объекта в строку. Вызов произойдет когда hook инспектируется | ||
|
||
```js | ||
const useCustomhook = () => { | ||
const [state, setState] = useState("name"); | ||
useDebugValue( | ||
`custom hook, ${state}`, | ||
str => `${str}, ${new Date().toDateString()}` | ||
); | ||
return [state, setState]; | ||
}; | ||
``` | ||
|
||
<!--v--> | ||
|
||
## useDebugValue. Пример | ||
1. Рассмотрим hook [useMouseDown](https://github.com/nickovchinnikov/minesweeper/blob/2ad019ff5be521f4e42f0793f69437f31a5a3555/src/components/hooks/useMouseDown.ts): | ||
<ul> | ||
<li>его реализацию</li> | ||
<li>для чего используется</li> | ||
<li>тесты</li> | ||
<li>пример использования useDebugValue</li> | ||
</ul> | ||
|
||
<!--v--> | ||
|
||
## useCustomHooks | ||
|
||
Функция, к которой применяются те же правила, что и к встроенным в React. **Какие?** | ||
- Начинается с префикса **use** | ||
- Используется только в пользовательских hook функциях или функциональных компонентах | ||
- Вызов всегда происходит на верхнем уровне, без условий, циклов и т.п. | ||
|
||
<!--v--> | ||
|
||
## useCustomHooks. Примеры | ||
Более сложные примеры из проекта Minesweeper: | ||
- [hook useGame](https://github.com/nickovchinnikov/minesweeper/blob/2ad019ff5be521f4e42f0793f69437f31a5a3555/src/modules/GameWithHooks/useGame.ts) | ||
- [hook useSettings](https://github.com/nickovchinnikov/minesweeper/blob/2ad019ff5be521f4e42f0793f69437f31a5a3555/src/modules/GameWithHooks/useSettings.ts) | ||
- [hook useStatus](https://github.com/nickovchinnikov/minesweeper/blob/2ad019ff5be521f4e42f0793f69437f31a5a3555/src/modules/GameWithHooks/useStatus.ts) | ||
- [hook useTime](https://github.com/nickovchinnikov/minesweeper/blob/2ad019ff5be521f4e42f0793f69437f31a5a3555/src/modules/GameWithHooks/useTime.ts) | ||
- [тесты](https://github.com/nickovchinnikov/minesweeper/blob/2ad019ff5be521f4e42f0793f69437f31a5a3555/src/modules/GameWithHooks/useGame.test.ts) для hook'а useGame | ||
|
||
<!--v--> | ||
|
||
## Вопросы | ||
|
||
<!--v--> | ||
|
||
## Ссылка на опрос | ||
|
||
<!--v--> | ||
|
||
## Спасибо за внимание! |
1 change: 1 addition & 0 deletions
1
presentations/module3/3_functionalProgrammingMonades/lecture.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters