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.
In this repository23 other files it gives its agents.
- client-document-exportSkill
- cloudflare-turnstile-nextjsSkill
- ga4-analytics-nextjsSkill
- git-repo-privacy-auditSkill
- hardened-contact-form-nextjsSkill
- html-semantic-markupSkill
- multi-step-wizard-flowSkill
- nextjs-api-routesSkill
- nextjs-app-routerSkill
- nextjs-seo-metadataSkill
- nextjs-ui-foundationSkill
- next-themes-dark-modeSkill
- playwright-e2e-testingSkill
- react-components-patternsSkill
- recharts-dashboardSkill
- resend-email-nextjsSkill
- server-pdf-playwrightSkill
- shadcn-ui-setupSkill
- specialized-form-inputsSkill
- tailwind-css-v4Skill
- vitest-unit-testingSkill
- web-accessibility-wcagSkill
- zustand-persist-stateSkill
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
- Distinctive: line 13
Only call hooks at top level (not in conditions/loops)
- Very specific: line 41
Never read localStorage during initial render on server.
≈ “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.
Files
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 use | Avoid |
| Sync to URL, analytics, subscriptions | Deriving display state from props |
| Redirect after hydration | Replacing 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 renderExtract 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
useEffectto sync prop → state without clear reason (prefer key remount or derived value)- Missing cleanup on intervals/subscriptions
useCallbackon every handler "just in case"- Reading external mutable refs during render for UI decisions
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
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.

