본문으로 건너뛰기
cccskills
무료GitHub에 공개

debugging-react-hooks

Debug React hooks infinite loops, stale closures, and missing dependencies. Systematic approach.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

설치 방법 보기

포함된 파일(1)

  • SKILL.md2.9 KB

SKILL.md(원문)

설치하기 전에 에이전트에게 전달되는 지시 내용을 확인할 수 있습니다.

The Most Common Hook Bugs

Bug 1: Infinite useEffect Loop

Symptom: Component re-renders endlessly, console floods with logs.

Cause Pattern:

// BUG: object/array created in render → new reference every render
useEffect(() => {
  setData(processData(rawData));
}, [processData]); // processData re-created every render!

Fix:

// Move stable values outside component, or use useCallback/useMemo
const processData = useCallback((data) => {
  return data.map(item => ({ ...item, processed: true }));
}, []); // empty deps = stable reference

useEffect(() => {
  setData(processData(rawData));
}, [processData, rawData]);

Bug 2: Stale Closure

Symptom: Effect reads old state value. The count is always 0 in a timer.

// BUG: count is captured at effect creation time (0)
useEffect(() => {
  const id = setInterval(() => {
    console.log(count); // always 0!
    setCount(count + 1); // always 1!
  }, 1000);
  return () => clearInterval(id);
}, []); // empty deps = stale closure

Fix:

// Option A: functional update (doesn't need count in deps)
useEffect(() => {
  const id = setInterval(() => {
    setCount(prev => prev + 1); // reads current value
  }, 1000);
  return () => clearInterval(id);
}, []);

// Option B: include count in deps (but re-registers interval)
useEffect(() => {
  const id = setInterval(() => setCount(count + 1), 1000);
  return () => clearInterval(id);
}, [count]);

Bug 3: Missing Dependency Warning → Incorrect Fix

Bad fix:

// Silencing ESLint by disabling rule — WRONG
useEffect(() => {
  fetchUser(userId); // depends on userId but it's not in deps
  // eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Correct fix:

useEffect(() => {
  fetchUser(userId);
}, [userId]); // proper dependency

Debugging Checklist

  1. Add console.log to identify the loop:

    useEffect(() => {
      console.log('Effect ran, deps:', dep1, dep2);
    }, [dep1, dep2]);
    
  2. Check if a dep is an object/array:

    // Each render creates a new object! → new reference → triggers effect
    const options = { limit: 10 }; // bad
    const options = useMemo(() => ({ limit: 10 }), []); // good
    
  3. React DevTools Profiler:

    • "Highlight updates when components render" — shows what's re-rendering
  4. Why Did You Render library:

    npm install @welldone-software/why-did-you-render
    

Rules of Hooks Compliance

  • Only call hooks at the top level (not inside loops/conditions)
  • Only call hooks from React functions
  • Install eslint-plugin-react-hooks — let the linter catch issues

리뷰

아직 리뷰가 없습니다. 사용해 본 소감을 남겨 주세요.

댓글

로그인 없이 쓸 수 있습니다. 사용 후기, 질문, 작동하지 않은 환경 등을 편하게 남겨 주세요.

아직 댓글이 없습니다. 첫 댓글을 남겨 보세요.

보내면 바로 공개됩니다. 이메일 주소나 전화번호 같은 개인 정보는 쓰지 마세요. URL은 2개까지입니다.

0 / 1,000자

이 양식은 reCAPTCHA로 보호되며 Google 개인정보처리방침 및 서비스 약관이 적용됩니다.

같은 저장소의 스킬

anoa

무료

Drive a real browser from the command line — open pages, snapshot the interactive elements as refs, click and fill by ref, read text, run JS, screenshot. Use when a task needs a live browser: checking a page renders, walking a login or checkout flow, scraping something that only exists after JavaScript runs, or reproducing a UI bug.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

porcupine-md/jonggrang162026년 9월 15일 업데이트

Run linters/typecheckers to calculate health score.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

porcupine-md/jonggrang162026년 9월 15일 업데이트

Comprehensive UI/UX compliance check (A11y, performance, responsive).

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

porcupine-md/jonggrang162026년 9월 15일 업데이트

Infrastructure-first security audit (OWASP, secrets, dependencies).

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

porcupine-md/jonggrang162026년 9월 15일 업데이트

auth

무료

Setup authentication flow (register, login, session/JWT, middleware)

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

porcupine-md/jonggrang162026년 9월 15일 업데이트

Deep knowledge about deploying applications to AWS (EC2, ECS, S3, CloudFront, Cloud Run).

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

porcupine-md/jonggrang162026년 9월 15일 업데이트

porcupine-md의 스킬 모두 보기

이 스킬의 문제 신고