Skill站外收录 @skilllib/skill-2014279424261173250
component-refactoring
针对高复杂度 React 组件提供系统性重构能力,通过提取自定义 Hook 分离逻辑与视图、拆分嵌套 JSX 为独立子组件、简化条件分支结构、集中管理模态框状态等方式,降低代码耦合度与维护成本,适用于 cyclomatic 复杂度超 50 或文件行数超 300 的场景。
软件开发 AI 技能
Skills.MD
# Component Refactoring Skill
Refactor high-complexity React components with proven patterns and workflows.
**Complexity Threshold**: Components with **cyclomatic complexity > 50** or **line count > 300** should be candidates for refactoring.
**Use When**:
- `pnpm analyze-component` shows high complexity.
- Users ask for "code splitting", "hook extraction", or "cleanup".
- A component file exceeds 300 lines of code.
## Core Refactoring Patterns
### 1. Extract Custom Hooks
**Goal**: Separate UI from State/Logic.
**Before**:
```tsx
function UserList() {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
setLoading(true);
fetch('/api/users').then(data => {
setUsers(data);
setLoading(false);
});
}, []);
if (loading) return <Spinner />;
return <ul>{users.map(u => <li key={u.id}>{u.name}</li>)}</ul>;
}
```
**After**:
```tsx
// hooks/useUsers.ts
function useUsers() {
return useQuery({ queryKey: ['users'], queryFn: fetchUsers });
}
// UserList.tsx
function UserList() {
const { data: users, isLoading } = useUsers();
if (isLoading) return <Spinner />;
return <UserListView users={users} />;
}
```
### 2. Extract Sub-Components
**Goal**: Break down monolithic JSX.
**Before**:
```tsx
function Dashboard() {
return (
<div>
<header>...</header>
<aside>...</aside>
<main>
<section className="stats">...</section>
<section className="feed">...</section>
</main>
</div>
);
}
```
**After**:
```tsx
function Dashboard() {
return (
<Layout>
<DashboardHeader />
<DashboardSidebar />
<DashboardContent>
<StatsWidget />
<ActivityFeed />
</DashboardContent>
</Layout>
);
}
```
### 3. Simplify Conditional Logic
**Goal**: Reduce nesting and `if/else` checks implementation details.
- Use **Lookup Tables** (Maps/Objects) instead of Switch/If-Else chains.
- Use **Guard Clauses** (Early Returns) to avoid deep nesting.
### 4. Extract Modal Management
**Goal**: Centralize modal state and logic.
- Move modal definitions to a generic `<ModalManager />` or context if reused globally.
- Keep the `isOpen` state locally if specific to a single component, but extract the Modal content to a separate file.
## Workflow
1. **Analyze**: Run complexity analysis or review the file manually.
2. **Plan**: Identify seam lines (Logic vs UI, Section vs Section).
3. **Extract**: Create new files for hooks or components.
4. **Integrate**: Replace original code with imports.
5. **Verify**: Ensure functionality remains identical and tests pass.
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「component-refactoring」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次