agentleFS
LeonxLJXLeonxLJX/web-engineer-skills/skills/react-hooks-discipline/SKILL.md

react-hooks-discipline

Skill0 starsChanged 2 months agoMITView on GitHub

React hooks that survive code review — exhaustive dependency arrays, render purity, custom hook boundaries, and the refs/rules that prevent stale-closure bugs. Use when writing or reviewing React function components or custom hooks.

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 4 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Never mutate refs during render (read-only in render.

    line 12
  2. Very specific:

    Never call setState unconditionally in render or in the body of another component.

    line 13
  3. Distinctive:

    If you *must* omit a dependency, write why in a comment and prefer restructuring:

    line 28
  4. Distinctive:

    Every custom hook returns a stable shape (tuple or object — pick per codebase and stick to it).

    line 50
  5. Distinctive:

    Event → effect split: if logic runs "when the user clicks", it lives in the handler, not in an effect watching a state flag.

    line 69

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

React hooks discipline

Render purity is the whole game

A component render must be a pure function of props + state + context. Anything else belongs in effects or event handlers.

  • Never mutate refs during render (read-only in render; write in effects/handlers).
  • Never call setState unconditionally in render or in the body of another component.
  • Derived data → useMemo or plain computation, not a second useState kept in sync:
// ❌ duplicated state to keep in sync
const [fullName, setFullName] = useState('')
useEffect(() => setFullName(`${first} ${last}`), [first, last])

// ✅ derived
const fullName = `${first} ${last}`

Dependency arrays: be exhaustive, then be honest

  1. Enable eslint-plugin-react-hooks exhaustive-deps and treat warnings as errors.
  2. If you must omit a dependency, write why in a comment and prefer restructuring:
// ❌ lie: latest always wins, silently breaks subscription
useEffect(() => {
  const id = setInterval(() => poll(url), 5_000)
  return () => clearInterval(id)
}, []) // url "won't change" — until it does

// ✅ restructure: keep it fresh via ref, or include it
const urlRef = useRef(url)
useEffect(() => { urlRef.current = url }, [url])
useEffect(() => {
  const id = setInterval(() => poll(urlRef.current), 5_000)
  return () => clearInterval(id)
}, [])
  1. Objects/arrays/functions as deps re-fire every render — stabilize at the source (useMemo/useCallback at the producer, or hoist constants out).

Custom hooks: same rules as components

  • Every custom hook returns a stable shape (tuple or object — pick per codebase and stick to it).
  • Effects in custom hooks still clean up. If it subscribes, it unsubscribes — no exceptions.
  • One behavior per hook. useUserWithPostsAndComments is three hooks wearing a trench coat.

The patterns that prevent 90% of bugs

  • Latest-value ref for subscriptions/intervals that shouldn't re-arm (above).
  • Abort on unmount for fetches:
useEffect(() => {
  const ac = new AbortController()
  fetch(url, { signal: ac.signal })
    .then(setData)
    .catch((e) => e.name !== 'AbortError' && setError(e))
  return () => ac.abort()
}, [url])
  • Event → effect split: if logic runs "when the user clicks", it lives in the handler, not in an effect watching a state flag.

Anti-patterns

  • Conditional hooks or hooks in loops/conditions (rule of hooks — non-negotiable).
  • useEffect chains mirroring imperative flows (a → b → c). Model state, not steps.
  • Context value recreated every render without memoization — memoize the value object or split contexts.
  • Storing functions in state; store data, derive behavior.

Definition of done (agent checklist)

  • exhaustive-deps clean, deviations documented with reason
  • Every effect with a subscription/timer/fetch has cleanup
  • No state that is derivable from props/state
  • Keys on lists are stable ids
  • No ref writes during render

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.