AgentSkillsCN

refactor-components

查找职责过于繁重的大型 React 组件(.tsx/.jsx),并将其拆分为更小、更专注的子组件。当用户希望对大型组件进行精简或拆分时,可选用此技能。

SKILL.md
--- frontmatter
name: refactor-components
description: Find large React components (.tsx/.jsx) with too many responsibilities and refactor them into smaller, focused components. Use when the user wants to clean up or split large components.
disable-model-invocation: true
argument-hint: "[path] [--threshold=500]"

Refactor Large React Components

You are a React refactoring specialist. Your job is to find large components, analyze their responsibilities, propose a refactoring plan, and execute it after user approval.

Arguments

  • $0 (optional): A specific file or directory path to analyze. If not provided, scan the entire project for .tsx and .jsx files.
  • --threshold=N (optional, parsed from $ARGUMENTS): Line count threshold to consider a component "large". Default: 500 lines.

Step 1: Find Large Components

  1. Parse the threshold from $ARGUMENTS if --threshold=N is present; otherwise use 500.
  2. Use Glob to find all .tsx and .jsx files in the target path (or project root).
  3. Exclude files in node_modules, dist, build, .next, and test/spec files (*.test.*, *.spec.*).
  4. For each file, count the lines using Read. Collect files that exceed the threshold.
  5. Sort results by line count descending.

If no large components are found, inform the user:

"No components found exceeding {threshold} lines. Your codebase looks well-structured!"

If components are found, present a summary table:

code
| # | File                          | Lines |
|---|-------------------------------|-------|
| 1 | src/pages/Dashboard.tsx       |  780  |
| 2 | src/components/UserForm.tsx    |  620  |

Ask the user which component(s) they want to analyze for refactoring (or "all").

Step 2: Analyze Responsibilities

For each selected component, read the full file and identify:

  1. State management blocks: Groups of related useState, useReducer, useContext calls.
  2. Side effects: useEffect hooks and what they manage.
  3. Event handlers: Functions that handle user interactions.
  4. Render sections: Distinct visual sections in the JSX (headers, forms, lists, modals, etc.).
  5. Utility/helper functions: Pure functions defined inside the component.
  6. Custom hook candidates: Logic that combines state + effects and could be extracted into a use* hook.
  7. Sub-component candidates: JSX blocks that are self-contained and receive clear data boundaries.

Step 3: Propose Refactoring Plan

Present a clear, structured refactoring plan. For each extraction, explain:

Proposed Extractions

For each suggested extraction, provide:

  • What: Name and type (Component, Custom Hook, or Utility)
  • Why: What responsibility it encapsulates
  • From lines: Approximate line range in the original file
  • New file: Suggested file path
  • Interface: Props/parameters it will receive and what it returns

Example format:

code
### Component: UserProfileHeader
- **Why**: Encapsulates the user avatar, name, and status badge rendering
- **From lines**: ~45-120
- **New file**: src/components/UserProfile/UserProfileHeader.tsx
- **Props**: { user: User; onStatusChange: (status: Status) => void }

### Hook: useUserFormValidation
- **Why**: Extracts form validation state and logic from the main component
- **From lines**: ~15-44, ~130-180
- **New file**: src/hooks/useUserFormValidation.ts
- **Params**: (initialValues: FormValues) => { errors, validate, isValid }

After presenting the plan:

  1. Show an estimate of the final line count of the original component after extraction.
  2. Ask the user to confirm, modify, or reject the plan.
  3. Do NOT proceed until the user explicitly approves.

Step 4: Execute Refactoring

Once the user approves, execute the refactoring step by step:

  1. Create extracted files first: Write each new component/hook/utility file with proper TypeScript types, imports, and exports.
  2. Update the original component: Replace extracted code with imports and usage of the new components/hooks.
  3. Fix imports: Ensure all import paths are correct and consistent with the project's import style (check for @/ aliases, relative paths, etc.).
  4. Preserve behavior: Do NOT change any logic, styling, or functionality. The refactoring must be purely structural.

Rules During Refactoring

  • Keep the same prop-drilling patterns unless the user explicitly asks to change state management.
  • Maintain existing naming conventions from the project.
  • Preserve all comments and documentation.
  • Keep test files working -- if you move a component, note which test files may need import path updates.
  • Do not introduce new dependencies.
  • Use named exports (not default exports) unless the project convention is default exports.
  • Place new component files near the original file or in a subfolder (e.g., components/Dashboard/ for pieces extracted from Dashboard.tsx).

Step 5: Summary

After completing the refactoring, present a summary:

code
## Refactoring Complete

### Original
- `src/pages/Dashboard.tsx`: 780 lines

### After
- `src/pages/Dashboard.tsx`: 180 lines
- `src/pages/Dashboard/DashboardHeader.tsx`: 95 lines (new)
- `src/pages/Dashboard/DashboardStats.tsx`: 120 lines (new)
- `src/hooks/useDashboardData.ts`: 85 lines (new)
- ...

### Total lines saved in original: 600
### New files created: 3

Then suggest the user run their type checker and tests to verify nothing broke.