Files
2nd/10_Wiki/Topic_Programming/Coding/React_Refs_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

3.0 KiB

id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
id title category status source_trust_level verification_status created_at updated_at tags tech_stack applied_in aliases
react-refs-patterns React Refs 사용 패턴 Coding draft B conceptual 2026-05-09 2026-05-09
react
refs
dom
vibe-coding
language applicable_to
TypeScript / React 18+
Web
React Native
useRef
forwardRef
callback ref

React Refs 사용 패턴

ref 는 두 가지: (1) DOM 참조 — 측정/포커스/스크롤, (2) 렌더에 영향 안 주는 mutable 값 보관. ref.current 변경은 재렌더 trigger 안 함.

📖 핵심 개념

  • DOM 또는 자식 imperative API 노출 (focus, play, scrollTo 등).
  • 렌더 사이 보존하는 값 — previous value, timer id, mutable 카운터.
  • ref 변경은 재렌더 안 함 (state 가 아님).

💻 코드 패턴

DOM 측정

function AutoFocus() {
  const inputRef = useRef<HTMLInputElement>(null);
  useEffect(() => { inputRef.current?.focus(); }, []);
  return <input ref={inputRef} />;
}

Previous value

function usePrevious<T>(value: T): T | undefined {
  const ref = useRef<T>();
  useEffect(() => { ref.current = value; }, [value]);
  return ref.current;
}

Imperative handle 노출

type VideoHandle = { play: () => void; pause: () => void };

const Video = forwardRef<VideoHandle, { src: string }>((props, ref) => {
  const videoRef = useRef<HTMLVideoElement>(null);
  useImperativeHandle(ref, () => ({
    play: () => videoRef.current?.play(),
    pause: () => videoRef.current?.pause(),
  }), []);
  return <video ref={videoRef} src={props.src} />;
});

Callback ref (DOM 노드 변경 감지)

const measureRef = useCallback((node: HTMLDivElement | null) => {
  if (node) console.log('mounted', node.getBoundingClientRect());
}, []);
return <div ref={measureRef}>...</div>;

🤔 의사결정 기준

상황 useRef useState
DOM 참조
렌더에 반영해야 함
렌더와 무관한 mutable
timer/AbortController 핸들
자식 imperative 메서드 노출 useImperativeHandle

안티패턴

  • ref.current 변경 후 화면 갱신 기대: ref 는 재렌더 trigger 안 함. forceUpdate 또는 state 사용.
  • render 중 ref.current 읽기/쓰기: concurrent mode 에서 일관성 깨짐. effect 안에서.
  • forwardRef 안 쓰고 props.ref 로 받기: React 18 에서 안 됨 (React 19 에서 ref prop 가능).
  • 모든 mutable 데이터 ref 로: state 가 더 나음. ref 는 진짜 렌더 안 영향 줄 때.
  • ref 가 callback 으로 받는 노드의 cleanup 안 함: callback ref 는 unmount 시 null 받음. 거기서 정리.

🤖 LLM 활용 힌트

  • "DOM 측정/포커스/스크롤 = ref. 렌더 영향 = state" 강조.
  • forwardRef 작성 시 imperative handle 의 메서드 시그니처 명확히.

🔗 관련 문서