Skip to content

skillFrontendreact

React Hooks Patterns

Writes custom React hooks with honest dependency arrays, proper cleanup and no stale closures, and fixes effects that misuse useEffect.

npx workspacetemplates add react-hooks-patterns --tool claude-code

Writes 1 file to .claude/skills/react-hooks-patterns/

.claude/skills/react-hooks-patterns/SKILL.md27 lines · 1.8 KBFile
---
name: react-hooks-patterns
description: Writes custom React hooks with honest dependency arrays, proper cleanup and no stale closures, and fixes effects that misuse useEffect.
---

# React hooks patterns

Most effects should not exist. Write the ones that do so they are safe to run twice.

## Steps

1. For every `useEffect` ask what it synchronizes with. If the answer is "state with other state", delete it and compute the value during render, or use `useMemo` if it is costly. If the answer is "a user action", move it into the event handler.
2. For real synchronization (subscription, timer, network, DOM API) write setup and cleanup as a pair. Unsubscribe, clear timers, abort fetches with `AbortController`, and ignore results of outdated requests.
3. Make the dependency array complete. Never silence `react-hooks/exhaustive-deps`. If a dependency causes unwanted reruns, fix its source: move the object or function inside the effect, memoize it at the owner, or read the latest value through a ref (`useRef` updated in an effect) or `useEffectEvent` where available.
4. Prevent stale closures: use the functional form `setCount(c => c + 1)` when new state depends on old state, and do not capture props in long-lived callbacks without listing them.
5. Extract a custom hook only when logic repeats or hides a lifecycle. Name it `useThing`, return a stable shape (object for many values, tuple for two), and keep it free of JSX.
6. Verify in Strict Mode: mount, unmount, remount must leave no duplicate listeners or requests.

## Rules

- Hooks are called at the top level, never in conditions or loops.
- Do not fetch in effects if the project has a data library; use it.
- Add a test with `renderHook` for each new hook.

## Report

Effects removed, hooks extracted, bugs fixed and tests added.

What it does

Hook bugs are quiet: a callback reads yesterday's state, a subscription never unsubscribes, an effect runs twice in development and nobody knows why. This skill gives the agent a checklist that catches those before they ship.

It extracts repeated logic into small hooks with a clear contract, and moves work out of useEffect when an event handler or a derived value is the right tool.

Example

you › useWindowSize keeps reporting the old size inside our resize handler

agent › Cause: the handler closed over width from the first render and the effect had an empty dependency array. Rewrote it as a useWindowSize hook that subscribes once with a cleanup and stores size in state via the functional setter. Removed 2 effects in Header that only copied props into state. Added renderHook tests for mount, resize and unmount.

Questions

Why remove effects instead of fixing them?
An effect that mirrors state adds an extra render and a place for values to drift. Computing during render is simpler and correct.
Does it support React 17 and earlier?
The patterns work from 16.8. Features like useEffectEvent need a recent React, and the agent checks your version first.
  • Bundle Size Reduction

    Skilljavascript

    Free