React Hooks Pattern
Reuse stateful logic across components via custom hooks
When to Use
- Multiple components share the same stateful logic (e.g., data fetching, form state, media queries)
- You want to extract complex logic from a component to improve readability
- You need to compose behaviors without inheritance or render props
- You are using React 16.8+ (hooks are unavailable in class components)
Instructions
- Identify repeated stateful logic across two or more components.
- Extract it into a function prefixed with
use (e.g., useWindowSize, useFetch, useForm).
- The custom hook must call at least one built-in hook (
useState, useEffect, useCallback, etc.).
- Return only what the consumer needs — avoid over-exposing internal state.
- Name the hook descriptively after its behavior, not its implementation (
useMediaQuery not useEventListener).
- Keep hooks pure and side-effect-free at the call site — effects belong inside
useEffect.
- Document the hook's return type with TypeScript interfaces.
- Co-locate the hook file with its primary consumer or in a
hooks/ directory.
// Good: descriptive name, typed return, minimal surface area
function useWindowSize(): { width: number; height: number } {
const [size, setSize] = useState({ width: window.innerWidth, height: window.innerHeight });
useEffect(() => {
const handler = () => setSize({ width: window.innerWidth, height: window.innerHeight });
window.addEventListener('resize', handler);
return () => window.removeEventListener('resize', handler);
}, []);
return size;
}
Details
Custom hooks were introduced in React 16.8 to solve the code reuse problem that previously required higher-order components or render props. The key insight: hooks are just functions, and the use prefix is a convention that enables lint rules (react-hooks/rules-of-hooks) to enforce hook semantics.
Trade-offs:
- Hooks compose easily but debugging deep hook stacks can be harder than class-based patterns
- The
use naming convention is enforced by ESLint, not the runtime — calling a hook outside a component or another hook will cause runtime errors, not type errors
- React DevTools shows custom hook names in the component tree when they follow the
use prefix convention
When NOT to use:
- Logic that does not involve state or effects — extract as a plain utility function instead
- Logic specific to a single component that will never be reused
Related patterns:
- Provider Pattern — hooks often expose context via a
useXxx() wrapper
- Compound Pattern — hooks can manage shared state for compound component groups
Source
https://patterns.dev/react/hooks-pattern
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.
1---2name: react-hooks-pattern3description: React Hooks Pattern4metadata:5 source: https://skillmd.com/skills/intense-visions/react-hooks-pattern6---7# React Hooks Pattern89> Reuse stateful logic across components via custom hooks1011## When to Use1213- Multiple components share the same stateful logic (e.g., data fetching, form state, media queries)14- You want to extract complex logic from a component to improve readability15- You need to compose behaviors without inheritance or render props16- You are using React 16.8+ (hooks are unavailable in class components)1718## Instructions19201. Identify repeated stateful logic across two or more components.212. Extract it into a function prefixed with `use` (e.g., `useWindowSize`, `useFetch`, `useForm`).223. The custom hook must call at least one built-in hook (`useState`, `useEffect`, `useCallback`, etc.).234. Return only what the consumer needs — avoid over-exposing internal state.245. Name the hook descriptively after its behavior, not its implementation (`useMediaQuery` not `useEventListener`).256. Keep hooks pure and side-effect-free at the call site — effects belong inside `useEffect`.267. Document the hook's return type with TypeScript interfaces.278. Co-locate the hook file with its primary consumer or in a `hooks/` directory.2829```typescript30// Good: descriptive name, typed return, minimal surface area31function useWindowSize(): { width: number; height: number } {32 const [size, setSize] = useState({ width: window.innerWidth, height: window.innerHeight });33 useEffect(() => {34 const handler = () => setSize({ width: window.innerWidth, height: window.innerHeight });35 window.addEventListener('resize', handler);36 return () => window.removeEventListener('resize', handler);37 }, []);38 return size;39}40```4142## Details4344Custom hooks were introduced in React 16.8 to solve the code reuse problem that previously required higher-order components or render props. The key insight: hooks are just functions, and the `use` prefix is a convention that enables lint rules (react-hooks/rules-of-hooks) to enforce hook semantics.4546**Trade-offs:**4748- Hooks compose easily but debugging deep hook stacks can be harder than class-based patterns49- The `use` naming convention is enforced by ESLint, not the runtime — calling a hook outside a component or another hook will cause runtime errors, not type errors50- React DevTools shows custom hook names in the component tree when they follow the `use` prefix convention5152**When NOT to use:**5354- Logic that does not involve state or effects — extract as a plain utility function instead55- Logic specific to a single component that will never be reused5657**Related patterns:**5859- Provider Pattern — hooks often expose context via a `useXxx()` wrapper60- Compound Pattern — hooks can manage shared state for compound component groups6162## Source6364https://patterns.dev/react/hooks-pattern6566## Process67681. Read the instructions and examples in this document.692. Apply the patterns to your implementation, adapting to your specific context.703. Verify your implementation against the details and edge cases listed above.7172## Harness Integration7374- **Type:** knowledge — this skill is a reference document, not a procedural workflow.75- **No tools or state** — consumed as context by other skills and agents.7677## Success Criteria7879- The patterns described in this document are applied correctly in the implementation.80- Edge cases and anti-patterns listed in this document are avoided.