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
52 changes: 52 additions & 0 deletions keyword/chapter08/keyword.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
- **`Debounce`** 구글링 후 개념 정리 및 코드 작성해보기 🍠
- **`Debounce`** 개념 정리 🍠

**Debounce**는 연속적으로 발생하는 이벤트를 그룹화하여, 마지막 이벤트가 발생한 후 일정 시간이 지났을 때만 콜백 함수를 한 번 실행하는 기술

### 1. 작동 원리

1. 이벤트가 발생하면 설정된 `delay` 타이머를 시작
2. `delay`가 끝나기 전에 동일 이벤트가 또 발생하면, 기존 타이머를 취소(Clear)하고 다시 타이머를 시작
3. 더 이상 이벤트가 발생하지 않고 `delay` 시간이 경과하면 비로소 함수를 실행

**주요 용도:**

- **입력값 검증 (ID 중복 체크 등):** 타이핑이 완전히 끝났을 때 API 호출.
- **윈도우 리사이징:** 사용자가 창 크기 조절을 멈췄을 때 레이아웃 재계산.

- **`Debounce`** 코드 작성 🍠

```tsx
function debounce(fn, delay){
let timer;
return (...args) => {
clearTimeout(timer); // 이전 요청 취소
timer = setTimeout(() => fn(...args), delay); // 마지막 요청 예약
};
}
```

- **`Throttling`** 구글링 후 개념 정리 및 코드 작성해보기 🍠

- **`Throttling`** 개념 정리 🍠
이벤트가 아무리 많이 발생해도, 설정한 시간 간격(Tick) 내에는 최대 한 번만 실행하게 함-.
- **동작:** 실행 중에는 타이머가 끝날 때까지 추가 요청을 무시(Ignore).
- **핵심 코드:**
- 주요 용도:
* 스크롤 이벤트 (Infinite Scroll): 스크롤 중 계속해서 데이터를 불러오거나 위치를 계산해야 할 때.
* 마우스 이동 (Drag & Drop): 좌표 계산 빈도를 제한하여 성능 확보.
- **`Throttling`** 코드 작성 🍠

```tsx
function throttle(fn, delay) {
let timer = null;
return (...args) => {
if (timer) return; // 이미 타이머가 있다면 무시

fn(...args); // 즉시 실행
timer = setTimeout(() => {
timer = null; // 설정 시간이 지나면 타이머 초기화
}, delay);
};
}
```
24 changes: 24 additions & 0 deletions mission/chapter08-1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
73 changes: 73 additions & 0 deletions mission/chapter08-1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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 [Oxc](https://oxc.rs)
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/)

## React Compiler

The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation).

## 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 defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
// Other configs...

// 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,

// Other configs...
],
languageOptions: {
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
// other options...
},
},
])
```

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 defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
// Other configs...
// Enable lint rules for React
reactX.configs['recommended-typescript'],
// Enable lint rules for React DOM
reactDom.configs.recommended,
],
languageOptions: {
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
// other options...
},
},
])
```
23 changes: 23 additions & 0 deletions mission/chapter08-1/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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'
import { defineConfig, globalIgnores } from 'eslint/config'

export default defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
js.configs.recommended,
tseslint.configs.recommended,
reactHooks.configs.flat.recommended,
reactRefresh.configs.vite,
],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
},
])
13 changes: 13 additions & 0 deletions mission/chapter08-1/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="/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>chapter03</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Loading