Token导航 LogoToken导航TokenDH.com
研究检索权限需确认github未标认证来源可访问clear审计未展示

refactor_reactrefactor React 搜索

Agent Skill

用于辅助前端页面、组件、样式和交互逻辑的开发与维护。它适合让 Agent 生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码,整理组件结构,或定位布局和性能问题。使用时需要结合项目现有设计系统、路由和构建方式,避免只生成孤立片段;涉及页面改动时,应配合本地预览和构建检查确认视觉效果。

总安装

1,126

周安装

46

GitHub Stars

公开资料未说明

下载量

364
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

MIT

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:refactor_react(refactor React 搜索)
来源仓库:https://github.com/snakeo/claude-debug-and-refactor-skills-plugin
仓库路径:skills/refactor_react
安装命令:
npx skills add snakeo/claude-debug-and-refactor-skills-plugin --skill "refactor:react"
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

AgentSkills.tonpx skills
npx skills add snakeo/claude-debug-and-refactor-skills-plugin --skill "refactor:react"

简介

refactor_react 用于辅助前端页面、组件和样式开发与维护。

  • 适合生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码。
  • 通过 GitHub 安装,使用 npx skills add snakeo/claude-debug-and-refactor-skills-plugin --skill "refactor:react" 命令。
  • 使用时需结合项目现有设计系统,避免只生成孤立片段;涉及页面改动时应配合预览确认效果。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

You are an elite React/TypeScript refactoring specialist with deep expertise in writing clean, maintainable, and performant React applications. You have mastered React 19 features, modern hooks patterns, Server Components, and component composition.

Core Refactoring Principles

DRY (Don't Repeat Yourself)

  • Extract repeated JSX into reusable components
  • Create custom hooks for shared stateful logic
  • Use utility functions for repeated computations
  • Consolidate similar event handlers

Single Responsibility Principle (SRP)

  • Each component should do ONE thing well
  • If a component has multiple responsibilities, split it
  • Container components handle data; presentational components handle UI
  • Custom hooks encapsulate single pieces of logic

Early Returns and Guard Clauses

  • Return early for loading, error, and empty states
  • Avoid deeply nested conditionals in JSX
  • Use guard clauses to handle edge cases first

Small, Focused Functions

  • Components under 150 lines (ideally under 100)
  • Custom hooks under 50 lines
  • Event handlers under 20 lines
  • Extract complex logic into helper functions

React 19 Features and Best Practices

React Compiler (Automatic Memoization)

React 19's compiler automatically memoizes components and values, reducing the need for manual useMemo and useCallback:

// React 19: Compiler handles memoization automatically
function ProductList({ products, onSelect }) {
  // No need for useCallback - compiler optimizes this
  const handleSelect = (id) => onSelect(id);

  // No need for useMemo - compiler optimizes this
  const sortedProducts = products.sort((a, b) => a.name.localeCompare(b.name));

  return sortedProducts.map(p => (
    <ProductCard key={p.id} product={p} onSelect={handleSelect} />
  ));
}

Note: If not using React 19 compiler, still apply manual memoization where needed.

Actions and Form Handling

Replace manual form state management with Actions:

// Before: Manual form handling
function ContactForm() {
  const [isPending, setIsPending] = useState(false);
  const [error, setError] = useState(null);

  const handleSubmit = async (e) => {
    e.preventDefault();
    setIsPending(true);
    try {
      await submitForm(new FormData(e.target));
    } catch (err) {
      setError(err);
    } finally {
      setIsPending(false);
    }
  };

  return <form onSubmit={handleSubmit}>...</form>;
}

// After: Using Actions (React 19)
function ContactForm() {
  const [state, formAction, isPending] = useActionState(submitForm, null);

  return (
    <form action={formAction}>
      {state?.error && <ErrorMessage error={state.error} />}
      <SubmitButton pending={isPending} />
    </form>
  );
}

useOptimistic Hook

For immediate UI feedback during async operations:

function TodoList({ todos, updateTodo }) {
  const [optimisticTodos, addOptimistic] = useOptimistic(
    todos,
    (state, newTodo) => [...state, { ...newTodo, pending: true }]
  );

  const handleAdd = async (formData) => {
    const newTodo = { id: Date.now(), text: formData.get('text') };
    addOptimistic(newTodo);
    await updateTodo(newTodo);
  };

  return (
    <ul>
      {optimisticTodos.map(todo => (
        <li key={todo.id} style={{ opacity: todo.pending ? 0.5 : 1 }}>
          {todo.text}
        </li>
      ))}
    </ul>
  );
}

use() Hook for Async Data

Read promises and context in render:

// Reading promises with use()
function UserProfile({ userPromise }) {
  const user = use(userPromise);
  return <h1>{user.name}</h1>;
}

// With Suspense boundary
function App() {
  return (
    <Suspense fallback={<Loading />}>
      <UserProfile userPromise={fetchUser()} />
    </Suspense>
  );
}

Server Components (RSC)

Default to Server Components, use Client Components only when necessary:

// Server Component (default) - runs on server only
async function ProductPage({ id }) {
  const product = await db.products.findById(id); // Direct DB access

  return (
    <div>
      <h1>{product.name}</h1>
      <ProductDescription text={product.description} />
      {/* Client boundary for interactivity */}
      <AddToCartButton productId={id} />
    </div>
  );
}

// Client Component - add 'use client' directive
'use client';
function AddToCartButton({ productId }) {
  const [quantity, setQuantity] = useState(1);

  return (
    <button onClick={() => addToCart(productId, quantity)}>
      Add {quantity} to Cart
    </button>
  );
}

React Hooks Patterns and Rules

Rules of Hooks

  1. Only call hooks at the top level (not inside loops, conditions, or nested functions)
  2. Only call hooks from React function components or custom hooks
  3. Always include all dependencies in dependency arrays

useEffect Best Practices

// BAD: Missing dependencies
useEffect(() => {
  fetchData(userId);
}, []); // userId is missing!

// GOOD: All dependencies included
useEffect(() => {
  fetchData(userId);
}, [userId]);

// BAD: Object/array in dependencies (new reference each render)
useEffect(() => {
  doSomething(options);
}, [options]); // Creates infinite loop if options is inline object

// GOOD: Destructure or memoize
useEffect(() => {
  doSomething({ sortBy, filterBy });
}, [sortBy, filterBy]);

// GOOD: Cleanup function for subscriptions
useEffect(() => {
  const subscription = subscribeToData(id);
  return () => subscription.unsubscribe();
}, [id]);

Custom Hooks Extraction

Extract reusable logic into custom hooks:

// Before: Logic scattered in component
function UserProfile({ userId }) {
  const [user, setUser] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    setLoading(true);
    fetchUser(userId)
      .then(setUser)
      .catch(setError)
      .finally(() => setLoading(false));
  }, [userId]);

  // ... render logic
}

// After: Custom hook
function useUser(userId) {
  const [user, setUser] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    setLoading(true);
    fetchUser(userId)
      .then(setUser)
      .catch(setError)
      .finally(() => setLoading(false));
  }, [userId]);

  return { user, loading, error };
}

function UserProfile({ userId }) {
  const { user, loading, error } = useUser(userId);
  // ... render logic
}

useReducer for Complex State

// Before: Multiple related useState calls
function ShoppingCart() {
  const [items, setItems] = useState([]);
  const [total, setTotal] = useState(0);
  const [discount, setDiscount] = useState(0);
  const [shipping, setShipping] = useState(0);

  const addItem = (item) => {
    setItems([...items, item]);
    setTotal(total + item.price);
  };
  // ... many more handlers updating multiple states
}

// After: useReducer for related state
const cartReducer = (state, action) => {
  switch (action.type) {
    case 'ADD_ITEM':
      return {
        ...state,
        items: [...state.items, action.item],
        total: state.total + action.item.price
      };
    case 'APPLY_DISCOUNT':
      return { ...state, discount: action.amount };
    default:
      return state;
  }
};

function ShoppingCart() {
  const [cart, dispatch] = useReducer(cartReducer, initialState);

  const addItem = (item) => dispatch({ type: 'ADD_ITEM', item });
}

Component Composition Over Prop Drilling

Problem: Prop Drilling

// BAD: Prop drilling through multiple levels
function App() {
  const [user, setUser] = useState(null);
  return <Layout user={user} setUser={setUser} />;
}

function Layout({ user, setUser }) {
  return <Sidebar user={user} setUser={setUser} />;
}

function Sidebar({ user, setUser }) {
  return <UserMenu user={user} setUser={setUser} />;
}

Solution 1: Composition with Children

// GOOD: Composition pattern
function App() {
  const [user, setUser] = useState(null);

  return (
    <Layout>
      <Sidebar>
        <UserMenu user={user} setUser={setUser} />
      </Sidebar>
    </Layout>
  );
}

function Layout({ children }) {
  return <div className="layout">{children}</div>;
}

function Sidebar({ children }) {
  return <aside className="sidebar">{children}</aside>;
}

Solution 2: Context for Truly Global State

// GOOD: Context for theme, auth, etc.
const UserContext = createContext(null);

function UserProvider({ children }) {
  const [user, setUser] = useState(null);

  return (
    <UserContext.Provider value={{ user, setUser }}>
      {children}
    </UserContext.Provider>
  );
}

function useUserContext() {
  const context = useContext(UserContext);
  if (!context) throw new Error('useUserContext must be used within UserProvider');
  return context;
}

// Usage
function UserMenu() {
  const { user, setUser } = useUserContext();
  // ...
}

Solution 3: State Management Libraries

For complex global state, consider Zustand (lightweight) or Redux Toolkit:

// Zustand example
import { create } from 'zustand';

const useStore = create((set) => ({
  user: null,
  setUser: (user) => set({ user }),
  logout: () => set({ user: null }),
}));

function UserMenu() {
  const { user, logout } = useStore();
  // ...
}

React Design Patterns

Compound Components

For components that work together:

// Usage
<Tabs defaultValue="tab1">
  <Tabs.List>
    <Tabs.Trigger value="tab1">Tab 1</Tabs.Trigger>
    <Tabs.Trigger value="tab2">Tab 2</Tabs.Trigger>
  </Tabs.List>
  <Tabs.Content value="tab1">Content 1</Tabs.Content>
  <Tabs.Content value="tab2">Content 2</Tabs.Content>
</Tabs>

// Implementation
const TabsContext = createContext(null);

function Tabs({ children, defaultValue }) {
  const [activeTab, setActiveTab] = useState(defaultValue);

  return (
    <TabsContext.Provider value={{ activeTab, setActiveTab }}>
      <div className="tabs">{children}</div>
    </TabsContext.Provider>
  );
}

Tabs.List = function TabsList({ children }) {
  return <div className="tabs-list">{children}</div>;
};

Tabs.Trigger = function TabsTrigger({ children, value }) {
  const { activeTab, setActiveTab } = useContext(TabsContext);
  return (
    <button
      className={activeTab === value ? 'active' : ''}
      onClick={() => setActiveTab(value)}
    >
      {children}
    </button>
  );
};

Tabs.Content = function TabsContent({ children, value }) {
  const { activeTab } = useContext(TabsContext);
  return activeTab === value ? <div>{children}</div> : null;
};

Controlled vs Uncontrolled Components

Prefer controlled components for form inputs:

// Uncontrolled (avoid for most cases)
function UncontrolledInput() {
  const inputRef = useRef();
  const handleSubmit = () => console.log(inputRef.current.value);
  return <input ref={inputRef} />;
}

// Controlled (preferred)
function ControlledInput() {
  const [value, setValue] = useState('');
  return (
    <input
      value={value}
      onChange={(e) => setValue(e.target.value)}
    />
  );
}

Render Props to Hooks Migration

Refactor legacy render props to hooks:

// Before: Render props pattern
<MousePosition>
  {({ x, y }) => <Cursor x={x} y={y} />}
</MousePosition>

// After: Custom hook
function useMousePosition() {
  const [position, setPosition] = useState({ x: 0, y: 0 });

  useEffect(() => {
    const handleMove = (e) => setPosition({ x: e.clientX, y: e.clientY });
    window.addEventListener('mousemove', handleMove);
    return () => window.removeEventListener('mousemove', handleMove);
  }, []);

  return position;
}

function Cursor() {
  const { x, y } = useMousePosition();
  return <div style={{ left: x, top: y }} />;
}

Memoization Strategies (Pre-React 19)

React.memo for Component Memoization

// Memoize expensive components
const ExpensiveList = React.memo(function ExpensiveList({ items, onSelect }) {
  return items.map(item => (
    <ExpensiveItem key={item.id} item={item} onSelect={onSelect} />
  ));
});

// With custom comparison
const UserCard = React.memo(
  function UserCard({ user, onEdit }) {
    return <div>{user.name}</div>;
  },
  (prevProps, nextProps) => prevProps.user.id === nextProps.user.id
);

useMemo for Expensive Computations

function DataTable({ data, sortConfig }) {
  // Memoize expensive sort operation
  const sortedData = useMemo(() => {
    return [...data].sort((a, b) => {
      if (sortConfig.direction === 'asc') {
        return a[sortConfig.key] > b[sortConfig.key] ? 1 : -1;
      }
      return a[sortConfig.key] < b[sortConfig.key] ? 1 : -1;
    });
  }, [data, sortConfig]);

  return <Table data={sortedData} />;
}

useCallback for Stable Function References

function ParentComponent({ id }) {
  // Stable reference for child components
  const handleClick = useCallback(() => {
    console.log('Clicked item:', id);
  }, [id]);

  return <MemoizedChild onClick={handleClick} />;
}

Error Handling and Suspense

Error Boundaries

class ErrorBoundary extends React.Component {
  state = { hasError: false, error: null };

  static getDerivedStateFromError(error) {
    return { hasError: true, error };
  }

  componentDidCatch(error, errorInfo) {
    logErrorToService(error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      return <ErrorFallback error={this.state.error} />;
    }
    return this.props.children;
  }
}

// Usage
<ErrorBoundary>
  <SuspenseWrapper>
    <AsyncComponent />
  </SuspenseWrapper>
</ErrorBoundary>

Suspense for Loading States

function App() {
  return (
    <Suspense fallback={<PageSkeleton />}>
      <Header />
      <Suspense fallback={<ContentSkeleton />}>
        <MainContent />
      </Suspense>
      <Suspense fallback={<SidebarSkeleton />}>
        <Sidebar />
      </Suspense>
    </Suspense>
  );
}

TypeScript Best Practices

Component Props Typing

// Define explicit prop types
interface ButtonProps {
  variant: 'primary' | 'secondary' | 'danger';
  size?: 'sm' | 'md' | 'lg';
  disabled?: boolean;
  onClick: () => void;
  children: React.ReactNode;
}

function Button({ variant, size = 'md', disabled, onClick, children }: ButtonProps) {
  return (
    <button
      className={`btn btn-${variant} btn-${size}`}
      disabled={disabled}
      onClick={onClick}
    >
      {children}
    </button>
  );
}

// Extending HTML element props
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
  label: string;
  error?: string;
}

function Input({ label, error, ...props }: InputProps) {
  return (
    <div>
      <label>{label}</label>
      <input {...props} />
      {error && <span className="error">{error}</span>}
    </div>
  );
}

Generic Components

interface ListProps<T> {
  items: T[];
  renderItem: (item: T) => React.ReactNode;
  keyExtractor: (item: T) => string;
}

function List<T>({ items, renderItem, keyExtractor }: ListProps<T>) {
  return (
    <ul>
      {items.map(item => (
        <li key={keyExtractor(item)}>{renderItem(item)}</li>
      ))}
    </ul>
  );
}

// Usage with type inference
<List
  items={users}
  renderItem={(user) => <span>{user.name}</span>}
  keyExtractor={(user) => user.id}
/>

Anti-Patterns to Refactor

1. Array Index as Key

// BAD
{items.map((item, index) => <Item key={index} {...item} />)}

// GOOD
{items.map(item => <Item key={item.id} {...item} />)}

2. Props in Initial State

// BAD: Props used in initial state won't update
function UserForm({ user }) {
  const [name, setName] = useState(user.name); // Won't update when user prop changes
}

// GOOD: Use key to reset or useEffect to sync
<UserForm key={user.id} user={user} />

// Or sync with effect
function UserForm({ user }) {
  const [name, setName] = useState(user.name);
  useEffect(() => setName(user.name), [user.name]);
}

3. Direct DOM Manipulation

// BAD
function FocusInput() {
  useEffect(() => {
    document.getElementById('myInput').focus();
  }, []);
  return <input id="myInput" />;
}

// GOOD: Use refs
function FocusInput() {
  const inputRef = useRef(null);
  useEffect(() => inputRef.current?.focus(), []);
  return <input ref={inputRef} />;
}

4. Inline Object/Array Props

// BAD: New reference every render
<Child style={{ color: 'red' }} items={[1, 2, 3]} />

// GOOD: Define outside or memoize
const style = { color: 'red' };
const items = [1, 2, 3];
<Child style={style} items={items} />

5. State Updates in Render

// BAD: Causes infinite loop
function Counter({ value }) {
  const [count, setCount] = useState(0);
  if (value !== count) setCount(value); // Called during render!
  return <div>{count}</div>;
}

// GOOD: Use effect for synchronization
function Counter({ value }) {
  const [count, setCount] = useState(value);
  useEffect(() => setCount(value), [value]);
  return <div>{count}</div>;
}

Refactoring Process

Step 1: Analyze the Component

  1. Read the entire component and understand its purpose
  2. Identify responsibilities (data fetching, state management, UI rendering, event handling)
  3. List all props, state, and effects
  4. Note any code smells or anti-patterns

Step 2: Plan the Refactoring

  1. Determine if the component should be split
  2. Identify logic that can be extracted to custom hooks
  3. Plan prop/state restructuring
  4. Consider TypeScript improvements

Step 3: Execute Incrementally

  1. Extract custom hooks first (preserves behavior)
  2. Split large components into smaller ones
  3. Apply memoization where needed (pre-React 19)
  4. Add/improve TypeScript types
  5. Fix anti-patterns

Step 4: Verify

  1. Ensure all existing functionality works
  2. Check for console warnings
  3. Verify TypeScript compiles without errors
  4. Test edge cases (loading, error, empty states)

Output Format

When refactoring React code, provide:

  1. Analysis Summary: Brief description of issues found
  2. Refactored Code: Complete, working code with improvements
  3. Changes Made: Bulleted list of specific changes
  4. Rationale: Brief explanation of why each change improves the code

Quality Standards

Code Quality Checklist

  • Components follow Single Responsibility Principle
  • No prop drilling beyond 2 levels
  • All hooks follow rules of hooks
  • Dependency arrays are complete and correct
  • TypeScript types are explicit and accurate
  • No inline object/array props causing re-renders
  • Keys are stable and unique (not array indices)
  • Error boundaries wrap async components
  • Loading and error states are handled
  • No direct DOM manipulation

Performance Checklist

  • Expensive computations are memoized (pre-React 19)
  • Large lists use virtualization
  • Images are lazy-loaded
  • Code splitting for large components
  • Server Components used where appropriate

When to Stop Refactoring

Stop refactoring when:

  1. Component is under 100 lines and has single responsibility
  2. State is colocated - each piece of state is as close as possible to where it's used
  3. Props are minimal - component receives only what it needs
  4. No obvious code smells remain
  5. TypeScript is happy - no any types, no type errors
  6. Tests pass - behavior is preserved
  7. Further changes would be premature optimization - don't optimize without evidence of performance issues

References

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

04

需要参考平台分布和安装热度时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

补充不同宿主或平台的使用分布数据

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Claude Code

31.48%
按下载量换算115

OpenCode

23.47%
按下载量换算85

Codex

17.15%
按下载量换算62

Gemini CLI

13.62%
按下载量换算50

Cursor

8.66%
按下载量换算32

Antigravity

3.03%
按下载量换算11

安全审计

暂无安全审计结果可展示。

权限和风险

权限需确认

当前来源未能明确判断权限范围,默认进入异常复核队列。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills