agentleFS
aiFabricoComaiFabricoCom/fabrico-collections/.claude/skills/fabrico-writing-hooks/SKILL.md

fabrico-writing-hooks

Skill2 starsChanged 4 months agoMITView on GitHub

Custom hook and composable patterns — naming, composition, stable return shapes, lifecycle cleanup, and testing strategies. Use when writing reusable logic units (React hooks, Vue composables), refactoring logic into hooks, debugging hook behavior, or reviewing hook implementations.

The file

We split this skill into the rules it gives an agent

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈9

    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:

    Every subscription, timer, event listener, or async operation created in an effect must have a corresponding cleanup.

    line 23
  2. Distinctive:

    Use the checklist below and track progress:

    line 30
  3. Very specific:

    Naming: Follow the project's framework convention for reusable logic units.

    line 44
  4. Distinctive:

    Inputs: Prefer a single options object when the hook accepts 3+ parameters.

    line 55
  5. Distinctive:

    Follow these rules during implementation:

    line 117
  6. Distinctive:

    Call hooks/composables at the top level only — never inside conditions, loops, or nested functions.

    line 121
  7. Distinctive:

    Ensure all reactive dependencies are properly tracked — whether through explicit dependency arrays or automatic reactivity tracking, …

    line 122
  8. Distinctive:

    Avoid object or array literals in dependency declarations — they create new identities every render/update cycle.

    line 123
  9. Distinctive:

    Use the framework's hook/composable testing utility.

    line 155
  10. Distinctive:

    Prefer objects over tuples for hooks returning more than one value.

    line 184

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 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

Writing Hooks

Provides patterns for writing composable, testable custom hooks with stable APIs, proper effect lifecycle management, and clear responsibility boundaries.

This skill uses "hook" to refer to any reusable, composable logic unit — React hooks, Vue composables, or equivalent framework abstractions. Adapt naming conventions and specific APIs to the project's framework.

<principles><single-responsibility> Each hook does one thing. Compose complex behavior by calling multiple hooks, not by adding optional parameters to a single hook. A hook with 5+ configuration options is a sign it needs decomposition. </single-responsibility><stable-api-surface> Prefer returning an object (rather than a tuple) once the hook returns more than 1–2 values, and keep references stable. Memoize callbacks and derived values using the framework's memoization primitives when consumers may pass them to memoized children. The hook's return shape is its public API — treat it as a contract. </stable-api-surface><cleanup-everything> Every subscription, timer, event listener, or async operation created in an effect must have a corresponding cleanup. Leaked resources cause memory leaks and stale state bugs that are hard to diagnose. </cleanup-everything></principles>

Hook Development Process

Use the checklist below and track progress:

Progress:
- [ ] Step 1: Define the hook's contract
- [ ] Step 2: Classify the hook tier
- [ ] Step 3: Implement with lifecycle discipline
- [ ] Step 4: Test the hook

Step 1: Define the hook's contract

Define the hook's public API before writing implementation code.

Naming: Follow the project's framework convention for reusable logic units. In React and Vue, the convention is use prefix + descriptive verb or noun. The name reflects behavior, not implementation details.

PatternExampleUse case
use + nounuseDebounce, useMediaQueryReturns a derived or observed value
use + verbuseToggle, useFetchReturns value + action(s)
use + domainuseAuth, useCartDomain-specific composition hook

Good: useLocalStorage — describes what it does. Bad: useBrowserEffect — describes how it works.

Inputs: Prefer a single options object when the hook accepts 3+ parameters. This makes the API extensible without breaking existing call sites.

// Prefer — single options object
const useDebounce = ({ value, delay, leading }: UseDebounceOptions) => {
  // ...
};

// Avoid — positional args are fragile past 2 params
const useDebounce = (value: string, delay: number, leading?: boolean) => {
  // ...
};

Outputs: Return an object with clear field names. Document what the hook returns and when it triggers effects.

interface UseAsyncReturn<T> {
  data: T | undefined;
  error: Error | null;
  isPending: boolean;
  refetch: () => void;
}

Step 2: Classify the hook tier

Determine where the hook fits in the dependency hierarchy. This classification constrains what a hook may depend on and prevents circular or inappropriate coupling.

TierNameScopeDependenciesExamples
AtomUI hooksLocal state, DOM events, media queries, keyboardNo remote data. No data-fetching libraries.useToggle, useMediaQuery, useKeyPress
MoleculeCoordination hooksCompose atom hooks with light coordination logicOther atom hooks only. No remote data.useFormField, usePagination
OrganismData hooksWrap data-fetching/mutation libraries. Domain logic.May call UI hooks internally. Interact with server cache.useUserProfile, useSearchResults

Rules:

  • Atom hooks must never import data-fetching or state management libraries.
  • Molecule hooks compose atoms — they do not call data hooks.
  • Organism hooks may call any tier but are the only tier that touches remote data.

Composability example — molecule hook composing atoms:

// Atom: single-purpose, no data fetching
const useDebounce = (value: string, delay: number) => {
  /* ... */
};
const useLocalStorage = <T>(key: string, initial: T) => {
  /* ... */
};

// Molecule: composes atoms into a higher-level behavior
const useDebouncedSearch = (storageKey: string) => {
  const { value, setValue } = useLocalStorage(storageKey, "");
  const debouncedValue = useDebounce(value, 300);
  return { query: value, setQuery: setValue, debouncedQuery: debouncedValue };
};

Step 3: Implement with lifecycle discipline

Follow these rules during implementation:

Call rules:

  • Call hooks/composables at the top level only — never inside conditions, loops, or nested functions.
  • Ensure all reactive dependencies are properly tracked — whether through explicit dependency arrays or automatic reactivity tracking, depending on the framework. Suppressing framework lint rules for dependency tracking is a last resort, not a shortcut.
  • Avoid object or array literals in dependency declarations — they create new identities every render/update cycle. Lift constants to module scope or memoize them.

Lifecycle cleanup — every effect that creates a resource must clean it up:

ResourceSetupRequired cleanup
TimersetTimeout(cb, delay)clearTimeout(id)
Event listeneraddEventListener(event, handler)removeEventListener(event, handler)
Async operationCancellable operation with AbortController signalcontroller.abort()
Subscriptionsource.subscribe(handler)subscription.unsubscribe()

The cleanup must execute when the hook unmounts or when dependencies change. See the framework reference for the exact cleanup syntax.

Error handling:

  • Don’t throw for expected runtime errors from the render/setup phase — surface those via the return value ({ error }).
  • It is acceptable to throw during render/setup for programmer errors (for example, a useX hook used outside its Provider) or when integrating with Error Boundaries / Suspense.
  • Catch async errors inside effects and store them in state.

Conditional execution:

  • Data hooks should support an enabled flag to prevent wasteful operations when preconditions are not met.
const useUserProfile = ({ userId, enabled = true }: Options) => {
  // Skip data fetching when disabled — delegate to data fetching library's enabled/skip option
  // Return idle state: { data: undefined, isPending: false, error: null }
};

Step 4: Test the hook

Use the framework's hook/composable testing utility. Structure tests around four areas:

  1. Initial state — Verify the hook returns the expected default values on first render.
  2. State transitions — Trigger actions and assert the return value updates correctly.
  3. Effect cleanup — Unmount the hook and verify all resources are released (timers cleared, listeners removed, subscriptions cancelled).
  4. Error states — Simulate failures and verify the hook surfaces errors via its return value.

For data hooks: wrap the test in required providers (query client, context providers) and mock network responses.

For timer-dependent hooks: use fake timers to control time progression without real delays.

it("cleans up the timer on unmount", () => {
  // Enable fake timers (syntax varies by test runner)
  // Render the hook using the framework's testing utility
  // Unmount
  // Advance all pending timers — verify no leaked side effects
  // Restore real timers after test
});

Return Shape Guidelines

Return typeWhenExample
Single valueHook computes one thinguseDebounce(value, delay) → debouncedValue
ObjectMultiple related values or actions{ data, error, isPending, refetch }
Object (prefer over tuple)Readability and evolvability matter{ count, increment, decrement, reset }

Prefer objects over tuples for hooks returning more than one value. Objects are self-documenting, allow consumers to destructure only what they need, and evolve without breaking existing call sites.

Anti-Patterns

Anti-PatternInstead Do
Tuple return [value, setter] for complex hooksObject return { value, setValue, reset }
Effect with suppressed dependency trackingInclude all deps; restructure if cycle occurs
New object/array identity every renderMemoize or lift to module scope
Side effects in render phaseMove to lifecycle effect or event handler
Swallowing errors silentlyReturn { error } so consumer can handle
One mega-hook with 10 optionsCompose from smaller single-purpose hooks
Timer without cleanupAlways return () => clearTimeout(id)
Data hook without enabled flagSupport conditional fetching to avoid waste

Hook Quality Checklist

Hook:
- [ ] Name follows framework convention (e.g., `use` prefix), describes behavior (not implementation)
- [ ] Single responsibility — does one thing well
- [ ] Inputs: single options object for 3+ params
- [ ] Output: object with stable references (memoized where framework supports it)
- [ ] All dependency tracking complete — no linting suppression
- [ ] Effects clean up: timers, listeners, subscriptions, abort controllers
- [ ] Errors surfaced via return value, never thrown from render
- [ ] Tested: initial state, transitions, cleanup, error states

Framework-Specific Patterns

The patterns above are framework-agnostic. For framework-specific hook/composable syntax, load the appropriate reference:

  • React: See ./references/react-patterns.md — useEffect, useMemo, useCallback, dependency arrays, rules of hooks.

Connected Skills

  • fabrico-implementing-frontend — hooks are used within components; this skill covers component patterns
  • fabrico-optimizing-frontend — for memoization strategies and performance patterns in hooks
  • fabrico-reviewing-frontend — for hook-specific code review criteria
  • fabrico-implementing-forms — for form-related custom hooks (field state, validation triggers)

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.