Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions keyword/chapter10/UseCallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
- **`useCallabck`** 에 대하여 정리해주세요! 🍠

: 함수의 참조 값을 유지해서 불필요한 리렌더링 방지

메모이제이션된 콜백 함수, 즉 이미 생성된 함수를 반환하는 리액트 훅(특정 함수를 새로 만들지 않고 재사용하고 싶을 때 사용)

- React.memo 등으로 래핑된 자식 컴포넌트에 **불필요한 리렌더링**이 일어나는 것을 방지
- useEffect·useMemo 의존 배열에 함수를 넣어도, 참조가 바뀌지 않아 **불필요한 effect 재실행·재계산**을 막음

- **`memo`**에 대하여 정리해주세요!🍠

: 값을 캐싱해서, 불필요한 반복되는 계산 방지

컴포넌트 자체를 메모이제이션을 해서, 불필요한 리렌더링 방지

- 부모가 재렌더되어도, **props가 변하지 않으면** 자식 컴포넌트를 다시 그리지 않음
- 대량의 리스트나 복잡한 UI 컴포넌트를 렌더링할 때 성능 최적화

- **`useMemo`** 에 대하여 정리해주세요! 🍠

: **값(value)** 을 메모이제이션해서, **불필요한 반복 계산**을 방지함.

메모이제이션된 값, 즉 **의존성(deps)** 이 바뀌지 않으면 **이전 계산 결과를 재사용**하는 리액트 훅

- 대용량 필터·정렬·복잡한 수치 연산 등과 같은 연산을 매 렌더마다 다시 하지 않음
- **참조형 데이터**(객체·배열 등)를 생성할 때 === 비교가 깨지지 않도록 **안정적인 참조** 유지
- useMemo와 React.memo를 함께 쓰면,
- **값이 진짜 바뀌었을 때만** 자식 컴포넌트에 새로운 props를 전달
- **불필요한 자식 리렌더링**을 더 효과적으로 막을 수 있음
25 changes: 25 additions & 0 deletions mission/chapter10/useCallback/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.env
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
54 changes: 54 additions & 0 deletions mission/chapter10/useCallback/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:

```js
export default tseslint.config({
extends: [
// Remove ...tseslint.configs.recommended and replace with this
...tseslint.configs.recommendedTypeChecked,
// Alternatively, use this for stricter rules
...tseslint.configs.strictTypeChecked,
// Optionally, add this for stylistic rules
...tseslint.configs.stylisticTypeChecked,
],
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:

```js
// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'

export default tseslint.config({
plugins: {
// Add the react-x and react-dom plugins
'react-x': reactX,
'react-dom': reactDom,
},
rules: {
// other rules...
// Enable its recommended typescript rules
...reactX.configs['recommended-typescript'].rules,
...reactDom.configs.recommended.rules,
},
})
```
28 changes: 28 additions & 0 deletions mission/chapter10/useCallback/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
13 changes: 13 additions & 0 deletions mission/chapter10/useCallback/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Loading