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.
In this repository42 other files it gives its agents.
- docs/agents.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- fabrico-architecture-designingSkill
- fabrico-building-admin-dashboardsSkill
- fabrico-building-admin-resourcesSkill
- fabrico-building-lean-applicationsSkill
- fabrico-codebase-analysingSkill
- fabrico-code-reviewingSkill
- fabrico-creating-agentsSkill
- fabrico-creating-instructionsSkill
- fabrico-creating-promptsSkill
- fabrico-creating-skillsSkill
- fabrico-designing-multi-cloud-architectureSkill
- fabrico-e2e-testingSkill
- fabrico-engineering-promptsSkill
- fabrico-ensuring-accessibilitySkill
- fabrico-implementation-gap-analysingSkill
- fabrico-implementing-backendSkill
- fabrico-implementing-ci-cdSkill
- fabrico-implementing-formsSkill
- fabrico-implementing-frontendSkill
- fabrico-implementing-kubernetesSkill
- fabrico-implementing-observabilitySkill
- fabrico-implementing-terraform-modulesSkill
- fabrico-improving-codebasesSkill
- fabrico-improving-uiSkill
- fabrico-jira-task-formattingSkill
- fabrico-managing-secretsSkill
- fabrico-optimizing-cloud-costSkill
- fabrico-optimizing-frontendSkill
- fabrico-planning-migrationSkill
- fabrico-preparing-applications-for-aiSkill
- fabrico-preparing-applications-for-saasSkill
- fabrico-reverse-engineering-specSkill
- fabrico-reviewing-frontendSkill
- fabrico-sql-and-database-understandingSkill
- fabrico-task-analysingSkill
- fabrico-task-extractingSkill
- fabrico-task-quality-reviewingSkill
- fabrico-technical-context-discoveringSkill
- fabrico-transcript-processingSkill
- fabrico-ui-verifyingSkill
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
- Distinctive: line 23
Every subscription, timer, event listener, or async operation created in an effect must have a corresponding cleanup.
- Distinctive: line 30
Use the checklist below and track progress:
- Very specific: line 44
Naming: Follow the project's framework convention for reusable logic units.
- Distinctive: line 55
Inputs: Prefer a single options object when the hook accepts 3+ parameters.
- Distinctive: line 117
Follow these rules during implementation:
- Distinctive: line 121
Call hooks/composables at the top level only — never inside conditions, loops, or nested functions.
- Distinctive: line 122
Ensure all reactive dependencies are properly tracked — whether through explicit dependency arrays or automatic reactivity tracking, …
- Distinctive: line 123
Avoid object or array literals in dependency declarations — they create new identities every render/update cycle.
- Distinctive: line 155
Use the framework's hook/composable testing utility.
- Distinctive: line 184
Prefer objects over tuples for hooks returning more than one value.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 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
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 hookStep 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.
| Pattern | Example | Use case |
use + noun | useDebounce, useMediaQuery | Returns a derived or observed value |
use + verb | useToggle, useFetch | Returns value + action(s) |
use + domain | useAuth, useCart | Domain-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.
| Tier | Name | Scope | Dependencies | Examples |
| Atom | UI hooks | Local state, DOM events, media queries, keyboard | No remote data. No data-fetching libraries. | useToggle, useMediaQuery, useKeyPress |
| Molecule | Coordination hooks | Compose atom hooks with light coordination logic | Other atom hooks only. No remote data. | useFormField, usePagination |
| Organism | Data hooks | Wrap 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:
| Resource | Setup | Required cleanup |
| Timer | setTimeout(cb, delay) | clearTimeout(id) |
| Event listener | addEventListener(event, handler) | removeEventListener(event, handler) |
| Async operation | Cancellable operation with AbortController signal | controller.abort() |
| Subscription | source.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
useXhook 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
enabledflag 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:
- Initial state — Verify the hook returns the expected default values on first render.
- State transitions — Trigger actions and assert the return value updates correctly.
- Effect cleanup — Unmount the hook and verify all resources are released (timers cleared, listeners removed, subscriptions cancelled).
- 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 type | When | Example |
| Single value | Hook computes one thing | useDebounce(value, delay) → debouncedValue |
| Object | Multiple 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-Pattern | Instead Do |
Tuple return [value, setter] for complex hooks | Object return { value, setValue, reset } |
| Effect with suppressed dependency tracking | Include all deps; restructure if cycle occurs |
| New object/array identity every render | Memoize or lift to module scope |
| Side effects in render phase | Move to lifecycle effect or event handler |
| Swallowing errors silently | Return { error } so consumer can handle |
| One mega-hook with 10 options | Compose from smaller single-purpose hooks |
| Timer without cleanup | Always return () => clearTimeout(id) |
Data hook without enabled flag | Support 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 statesFramework-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 patternsfabrico-optimizing-frontend— for memoization strategies and performance patterns in hooksfabrico-reviewing-frontend— for hook-specific code review criteriafabrico-implementing-forms— for form-related custom hooks (field state, validation triggers)
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.

