agentleFS
bkjain655bkjain655/agent-skills/skills/react-hooks-patterns/SKILL.md

react-hooks-patterns

Skill0 starsChanged 3 months agoMITView on GitHub

React hooks best practices: useEffect dependencies, useMemo/useCallback, custom hooks, hydration guards, and store selectors. Use when writing React hooks, fixing stale closures, infinite loops, or SSR hydration mismatches.

The file

We split this skill into the rules it gives an agent

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    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:

    Only call hooks at top level (not in conditions/loops)

    line 13
  2. Very specific:

    Never read localStorage during initial render on server.

    line 41

≈ “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 Patterns

Rules of hooks

  • Only call hooks at top level (not in conditions/loops)
  • Custom hooks must start with use
  • ESLint react-hooks/exhaustive-deps — fix deps, don't disable blindly

useEffect — when and how

Good useAvoid
Sync to URL, analytics, subscriptionsDeriving display state from props
Redirect after hydrationReplacing event handlers
Fetch on mount (client-only)Whole-form validation on every keystroke via effect
useEffect(() => {
  if (!hydrated) return;
  if (!isAuthed) router.replace("/login");
}, [hydrated, isAuthed, router]);

Hydration guard (persisted client state)

Zustand/localStorage differs server vs client — gate UI until rehydrated:

const hydrated = useStoreHydrated(); // onRehydrateStorage sets flag
if (!hydrated) return <LoadingSkeleton />;

Never read localStorage during initial render on server.

useMemo / useCallback

const metrics = useMemo(() => computeMetrics(data), [data]);
const handleSave = useCallback(() => save(data), [data, save]);

Use when:

  • Expensive pure computation
  • Stable reference needed for memoized child deps

Skip when computation is trivial — premature memoization adds noise.

Custom hook template

export function useDebouncedValue<T>(value: T, ms = 300): T {
  const [debounced, setDebounced] = useState(value);
  useEffect(() => {
    const id = setTimeout(() => setDebounced(value), ms);
    return () => clearTimeout(id);
  }, [value, ms]);
  return debounced;
}

Colocate in hooks/ or next to the feature using it.

Store selectors (Zustand)

Narrow subscriptions — avoid re-render on unrelated slice changes:

const name = useStore((s) => s.profile.name);
// not: const store = useStore() then store.profile.name in render

Extract selector hooks for repeated access:

export const useProfile = () => useStore((s) => s.data.profile);

Refs for DOM / mutable values

const exportRef = useRef<HTMLDivElement>(null);
const ranOnce = useRef(false);

useEffect(() => {
  if (ranOnce.current) return;
  ranOnce.current = true;
  trackEvent("page_view");
}, []);

Anti-patterns

  • useEffect to sync prop → state without clear reason (prefer key remount or derived value)
  • Missing cleanup on intervals/subscriptions
  • useCallback on every handler "just in case"
  • Reading external mutable refs during render for UI decisions

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.