| 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
- Parse the threshold from
$ARGUMENTS if --threshold=N is present; otherwise use 500.
- Use
Glob to find all .tsx and .jsx files in the target path (or project root).
- Exclude files in
node_modules, dist, build, .next, and test/spec files (*.test.*, *.spec.*).
- For each file, count the lines using
Read. Collect files that exceed the threshold.
- 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:
| # | 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:
- State management blocks: Groups of related
useState, useReducer, useContext calls.
- Side effects:
useEffect hooks and what they manage.
- Event handlers: Functions that handle user interactions.
- Render sections: Distinct visual sections in the JSX (headers, forms, lists, modals, etc.).
- Utility/helper functions: Pure functions defined inside the component.
- Custom hook candidates: Logic that combines state + effects and could be extracted into a
use* hook.
- 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:
### 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:
- Show an estimate of the final line count of the original component after extraction.
- Ask the user to confirm, modify, or reject the plan.
- Do NOT proceed until the user explicitly approves.
Step 4: Execute Refactoring
Once the user approves, execute the refactoring step by step:
- Create extracted files first: Write each new component/hook/utility file with proper TypeScript types, imports, and exports.
- Update the original component: Replace extracted code with imports and usage of the new components/hooks.
- Fix imports: Ensure all import paths are correct and consistent with the project's import style (check for
@/ aliases, relative paths, etc.).
- 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:
## 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.