Agent skill
refactor-component
Refactor React components for better maintainability, performance, or patterns. Covers hook extraction, component splitting, type safety, memoization, and composition.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/refactor-component
SKILL.md
Refactor Component
Improve existing components without changing behavior.
Common Goals
- Extract reusable logic into hooks
- Split large components into sub-components
- Simplify complex conditionals
- Improve type safety
- Apply design tokens (remove arbitrary values)
- Add memoization for performance
Patterns
Extract Custom Hooks
// Before: logic mixed in component
// After:
function useUsers() {
const [users, setUsers] = useState([])
const [loading, setLoading] = useState(false)
// ... fetch logic
return { users, loading, error }
}
function UserList() {
const { users, loading, error } = useUsers()
// ... render only
}
Split Large Components
// Before: 200+ line component with everything inline
// After: composed sub-components
function ProductCard({ product }) {
return (
<Card>
<ProductImage src={product.image} alt={product.name} />
<ProductInfo name={product.name} description={product.description} />
<ProductActions price={product.price} />
</Card>
)
}
Simplify Conditionals with Config Objects
const statusConfig = {
pending: { color: 'yellow', label: 'Pending' },
approved: { color: 'green', label: 'Approved' },
} as const
function Status({ status }: { status: keyof typeof statusConfig }) {
const config = statusConfig[status]
return <Badge color={config.color}>{config.label}</Badge>
}
Improve Type Safety
// Before: untyped props
function Card({ user }) { ... }
// After: explicit interface
interface CardProps { user: User; onEdit?: (user: User) => void }
function Card({ user, onEdit }: CardProps) { ... }
Memoization
const processed = useMemo(() =>
items.filter(i => i.type === filter).sort((a, b) => a.name.localeCompare(b.name)),
[items, filter]
)
const Item = memo(({ name }: { name: string }) => <div>{name}</div>)
Composition over Props
// Before: flat prop drilling (title, content, footer)
// After: compound component
<Modal isOpen onClose={close}>
<Modal.Header>Title</Modal.Header>
<Modal.Body>Content</Modal.Body>
<Modal.Footer>Actions</Modal.Footer>
</Modal>
Checklist
- Tests still pass after refactor
- No regression in functionality
- Improved readability
- Better type safety
- Uses design tokens (no arbitrary values)
- Follows architectural layer rules
- Performance improved (if that was the goal)
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?