Files
2nd/10_Wiki/Topic_Programming/Frontend/Custom-Hooks-Patterns.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

6.9 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-custom-hooks-patterns Custom Hooks Patterns 10_Wiki/Topics verified self
React Custom Hooks
useXxx Patterns
none A 0.9 applied
react
hooks
patterns
2026-05-10 pending
language framework
TypeScript React 19

Custom Hooks Patterns

매 한 줄

"매 custom hook = stateful logic 의 재사용 unit, JSX 의 X.". React 19 (2024) use() + Server Components 시대에는 custom hook이 매 client-side state machine + side effect orchestration 의 primary abstraction. 매 naming은 useXxx — 매 ESLint react-hooks rule 의 enforce.

매 핵심

매 규칙 (Rules of Hooks)

  • Top-level 만 호출 — 매 conditional / loop X.
  • React function / 매 다른 hook 안에서 만 — 매 일반 function X.
  • Naming useXxx — 매 lint 가 의존.

매 composition

  • Hook = 매 다른 hook 의 조합 — useMutationuseState + useCallback + useRef.
  • 매 stateful logic 의 분리 + 매 testable 단위.

매 응용

  1. Data fetching — useQuery, useSWR.
  2. DOM observation — useIntersectionObserver, useResizeObserver.
  3. State machines — useToggle, useReducer wrapper.
  4. Browser API — useLocalStorage, useMediaQuery, useGeolocation.

💻 패턴

1. useToggle (state primitive)

import { useState, useCallback } from 'react';

export function useToggle(initial = false) {
  const [on, setOn] = useState(initial);
  const toggle = useCallback(() => setOn((v) => !v), []);
  const setOnTrue = useCallback(() => setOn(true), []);
  const setOnFalse = useCallback(() => setOn(false), []);
  return { on, toggle, setOnTrue, setOnFalse };
}

2. useDebouncedValue

import { useEffect, useState } from 'react';

export function useDebouncedValue<T>(value: T, delayMs = 300): T {
  const [debounced, setDebounced] = useState(value);
  useEffect(() => {
    const id = setTimeout(() => setDebounced(value), delayMs);
    return () => clearTimeout(id);
  }, [value, delayMs]);
  return debounced;
}

3. useEventListener (typed)

import { useEffect, useRef } from 'react';

export function useEventListener<K extends keyof WindowEventMap>(
  type: K,
  handler: (e: WindowEventMap[K]) => void,
  target: Window | HTMLElement = window,
) {
  const handlerRef = useRef(handler);
  useEffect(() => { handlerRef.current = handler; });
  useEffect(() => {
    const listener = (e: Event) => handlerRef.current(e as WindowEventMap[K]);
    target.addEventListener(type, listener);
    return () => target.removeEventListener(type, listener);
  }, [type, target]);
}

4. useIntersectionObserver

import { useEffect, useRef, useState } from 'react';

export function useIntersectionObserver<T extends Element>(
  options: IntersectionObserverInit = {},
) {
  const ref = useRef<T>(null);
  const [entry, setEntry] = useState<IntersectionObserverEntry | null>(null);
  useEffect(() => {
    if (!ref.current) return;
    const obs = new IntersectionObserver(([e]) => setEntry(e), options);
    obs.observe(ref.current);
    return () => obs.disconnect();
  }, [options.root, options.rootMargin, options.threshold]);
  return { ref, entry, isVisible: entry?.isIntersecting ?? false };
}

5. useLocalStorage (with sync)

import { useEffect, useState } from 'react';

export function useLocalStorage<T>(key: string, initial: T) {
  const [value, setValue] = useState<T>(() => {
    const raw = localStorage.getItem(key);
    return raw ? (JSON.parse(raw) as T) : initial;
  });
  useEffect(() => {
    localStorage.setItem(key, JSON.stringify(value));
  }, [key, value]);
  useEffect(() => {
    const onStorage = (e: StorageEvent) => {
      if (e.key === key && e.newValue) setValue(JSON.parse(e.newValue));
    };
    window.addEventListener('storage', onStorage);
    return () => window.removeEventListener('storage', onStorage);
  }, [key]);
  return [value, setValue] as const;
}

6. useFetch (with AbortController)

import { useEffect, useState } from 'react';

export function useFetch<T>(url: string) {
  const [state, setState] = useState<{ data?: T; error?: Error; loading: boolean }>({ loading: true });
  useEffect(() => {
    const ac = new AbortController();
    setState({ loading: true });
    fetch(url, { signal: ac.signal })
      .then((r) => r.json())
      .then((data: T) => setState({ data, loading: false }))
      .catch((error: Error) => {
        if (error.name !== 'AbortError') setState({ error, loading: false });
      });
    return () => ac.abort();
  }, [url]);
  return state;
}

7. useReducer composite (state machine)

import { useReducer } from 'react';

type State = { status: 'idle' | 'loading' | 'success' | 'error'; data?: unknown; error?: Error };
type Action = { type: 'fetch' } | { type: 'success'; data: unknown } | { type: 'error'; error: Error };

const reducer = (s: State, a: Action): State => {
  switch (a.type) {
    case 'fetch': return { status: 'loading' };
    case 'success': return { status: 'success', data: a.data };
    case 'error': return { status: 'error', error: a.error };
  }
};

export const useAsync = () => useReducer(reducer, { status: 'idle' });

8. React 19 use() for promises

import { use, Suspense } from 'react';

function Profile({ promise }: { promise: Promise<User> }) {
  const user = use(promise); // suspends until resolved
  return <h1>{user.name}</h1>;
}

매 결정 기준

상황 Approach
Local boolean useToggle.
Server data useQuery (TanStack Query) — 매 wheel 재발명 X.
매 cross-tab sync useLocalStorage + storage event.
Async resource React 19 use() + Suspense.
복잡 state useReducer 매 state machine.
매 DOM measurement useResizeObserver, useIntersectionObserver.

기본값: 매 TanStack Query / Zustand 의 use — custom hook은 매 truly specific logic 만.

🔗 Graph

🤖 LLM 활용

언제: 매 hook scaffolding, TS generic 작성, 매 cleanup logic. 언제 X: 매 stale-closure / dep array 미세 bug — 매 manual review 필요.

안티패턴

  • Conditional hook call: 매 if (x) useFoo() — 매 lint error.
  • Stale closure in setInterval: 매 ref pattern 또는 functional setState 사용.
  • Effect for derived state: 매 useMemo 또는 render 중 계산 — useEffect X.
  • No cleanup: 매 listener / subscription 미해제 — leak.

🧪 검증 / 중복

  • Verified (react.dev, React 19 release notes).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — React 19 hook patterns + use()