Agent-skills-standard react-hooks
Write efficient React functional components and hooks. Use when writing custom hooks, optimizing useEffect, or working with useMemo/useCallback in React. (triggers: **/*.tsx, **/*.jsx, useEffect, useCallback, useMemo, useState, useRef, useContext, useReducer, useLayoutEffect, custom hook)
install
source · Clone the upstream repo
git clone https://github.com/HoangNguyen0403/agent-skills-standard
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HoangNguyen0403/agent-skills-standard "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/react/react-hooks" ~/.claude/skills/hoangnguyen0403-agent-skills-standard-react-hooks && rm -rf "$T"
manifest:
skills/react/react-hooks/SKILL.mdsource content
React Hooks Expert
Priority: P0 (CRITICAL)
Role: React Performance Expert. Optimize renders, prevent memory leaks.
Implementation Guidelines
- Dependency Arrays: exhaustive-deps Law. Objects/arrays recreated each render, causing infinite loops if not handled. Fix by ensuring objects/arrays memoized with
before putting them in deps, or usinguseMemo
for stable refs.useRef - Memoization: useMemo for heavy calc (expensive computed values) and useCallback for props (stabilize function references for memoized children). Measure first to avoid premature complexity.
- Custom Hooks: Extract logic starting with use... — use
for internal state and return only what's needed.useState
: Sync with external systems ONLY. Cleanup required for subscriptions/event listeners. Return cleanup function from effect. UseuseEffect
for fetch cleanup to prevent state updates after unmount.AbortController
: Mutable state without re-renders (DOM, timers, tracking).useRef
/useMemo
: Measure first. Use for stable refs or heavy computation.Callback- Stability: Use
pattern (ref) for event handlers to avoid dependency changes; see useLatest ref pattern example for reference implementation.useLatest - Concurrency:
/useTransition
for non-blocking UI updates.useDeferredValue - Initialization: Lazy state
.useState(() => expensive())
Performance Checklist (Mandatory)
- Rules of Hooks: Called at top level? No loops/conditions?
- Dependencies: objects/arrays memoized before passing to deps?
- Cleanup:
subscriptions return cleanup functions?useEffect - Render Count: component re-render excessively?
Anti-Patterns
- No Missing Deps: Fix logic, don't disable linter.
- No Complex Effects: Break tailored effects into smaller ones.
- No Derived State: Compute during render, don't
to sync state.useEffect - No Heavy Init: Use lazy state initialization
.useState(() => heavy())