AgentSkillsCN

Create Hook

按照Pera规范,创建新的React Hook。

SKILL.md
--- frontmatter
description: Create a new React hook following Pera conventions

Create Hook

1. Identify Hook Type

TypeSuffixWhen to Use
React Query (fetch)QueryFetching data from API
React Query (mutate)MutationCreating, updating, deleting data
Zustand StoreStoreLocal application state management
Component LogicComponent nameExtracting component/screen logic

2. Determine Location

ScopeLocation
Domain-level (shared)modules/[mod]/hooks/
Cross-domainmodules/[originDomain]/hooks/ (export via barrel)
Screen-specificmodules/[mod]/screens/[Screen]/use[Screen].ts
Component-specificSame folder as the component

Steps for React Query Hook

  1. Create hook file at modules/[mod]/hooks/use[Name]Query.ts or use[Name]Mutation.ts
  2. Define explicit param and result types (never expose UseQueryResult / UseMutationResult)
  3. Add to barrel file modules/[mod]/hooks/index.ts
  4. Create test at modules/[mod]/hooks/__tests__/use[Name]Query.spec.ts

See references/patterns.md for query and mutation examples.

Steps for Zustand Store Hook

  1. Create hook file at modules/[mod]/hooks/use[Name]Store.ts
  2. Use granular selectors, never destructure from useStore() directly
  3. Add to barrel file
  4. Create test

See references/store-patterns.md for store creation and access patterns.

Steps for Component/Screen Logic Hook

  1. Create hook file colocated with component:
    • AccountCard/useAccountCard.ts
    • AccountScreen/useAccountScreen.ts
  2. Extract all complex logic (useState, useMemo, useCallback, data fetching) from component
  3. Return a typed result object
  4. Update component to import and use the hook
  5. Create test

See references/patterns.md for component and screen hook examples.

Verification

sh
pnpm pre-push --no-fail-on-error
pnpm test

Checklists

Naming

  • Hook name starts with use prefix (camelCase)
  • Query hooks end with Query, mutation hooks with Mutation, store hooks with Store
  • File name matches hook name exactly

Type Decoupling

  • Input parameters have explicit type definitions
  • Return value has explicit type definition
  • Return type does NOT use dependency types
  • Only expose necessary properties to consumers