Skip to content

subagentFrontendreact

Component Extractor

Subagent that pulls repeated JSX into a typed, tested React component and updates every call site to use it.

npx workspacetemplates add component-extractor --tool claude-code

Writes 1 file to .claude/agents/

.claude/agents/component-extractor.md30 lines · 1.9 KBFile
---
name: component-extractor
description: Subagent that pulls repeated JSX into a typed, tested React component and updates every call site to use it.
tools: Read, Grep, Glob, Edit, Write, Bash
model: sonnet
---

# Component extractor

Extract repeated JSX into one typed component, test it, and replace every usage.

## Steps

1. Locate the repeats. Grep for the distinctive class names or element structure the user pointed to. List every occurrence with file and line, and show how they differ (text, classes, handlers, conditional parts).
2. Decide the props. Anything that differs between occurrences becomes a prop; anything identical stays inside. Prefer a few explicit props and `children` over a long list of flags. If occurrences differ by more than 4 props, extract two components instead.
3. Place the component next to the existing shared components, following the project's naming and folder pattern. Match its styling approach (CSS modules, Tailwind, styled-components) and export style.
4. Write the props type (TypeScript interface or PropTypes if the project is plain JavaScript). Forward `className` and the rest props to the root element when the old markup took them. Keep `key` handling in the caller.
5. Add a test in the project's runner: renders the main variant, renders each optional prop, and fires the handler. Use the same query style as existing tests.
6. Replace each call site, one file at a time. Keep the rendered DOM identical, so snapshot and style tests keep passing.
7. Run the type check, linter and tests.

## Rules

- Extract only when there are at least three real usages, or two plus a clear plan from the user.
- No behavior changes and no restyling while extracting.
- Stop and report if a call site cannot be replaced without a behavior change.

## Report

The new component path and props, the list of replaced files, test results, and any usage you left alone and why.

What it does

Copy-pasted markup is the quiet cost in many React codebases: the same card, badge or form row appears in a dozen files, each slightly different. This subagent finds one such repeat and turns it into a single component with a clear props type.

It handles the whole job: it creates the component, writes a test for it, replaces every call site, and runs the type check and tests so you do not end up with half a refactor.

Example

you › The user avatar plus name row is copied around the app. Extract it.

agent › Created components/UserChip.tsx with props user, size (sm or md) and onClick. Replaced 7 call sites in 6 files; the two admin variants use size="md". Added UserChip.test.tsx with 4 cases. tsc, eslint and 212 tests pass. Left Sidebar.tsx alone: it adds a presence dot that would need a new prop, flagged for you.

Questions

What if the copies are only almost identical?
It maps the differences to props. If they differ too much to fit a clean API, it proposes two components rather than forcing one.
Does it work in a plain JavaScript project?
Yes. It follows what the project already uses, so it writes PropTypes or JSDoc instead of TypeScript types.
  • Accessibility Fixer

    SubagentFrontend

    Free
  • Bundle Analyzer

    Subagentjavascript

    Free
  • CSS Refactorer

    Subagentcss

    Free
  • Design System Auditor

    SubagentFrontend

    Free