solid-react
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSOLID React - Component Architecture
SOLID React - 组件架构
Codebase Analysis (MANDATORY)
代码库分析(强制要求)
Before ANY implementation:
- Explore project structure to understand architecture
- Read existing related files to follow established patterns
- Identify naming conventions, coding style, and patterns used
- Understand data flow and dependencies
在进行任何实现之前:
- 探索项目结构以理解架构
- 阅读现有相关文件以遵循已确立的模式
- 识别所使用的命名规范、编码风格和模式
- 理解数据流和依赖关系
DRY - Reuse or Create Shared (MANDATORY)
DRY - 复用或创建共享代码(强制要求)
Before writing ANY new code:
- Grep the codebase for similar function names, patterns, or logic
- Check shared locations: ,
modules/cores/lib/,modules/cores/components/modules/cores/hooks/ - If similar code exists → extend/reuse instead of duplicate
- If code will be used by 2+ features → create it in directly
modules/cores/ - Extract repeated logic (3+ occurrences) into shared helpers
- Run after creating new files
npx jscpd ./src --threshold 3
在编写任何新代码之前:
- 在代码库中搜索相似的函数名、模式或逻辑
- 检查共享位置:、
modules/cores/lib/、modules/cores/components/modules/cores/hooks/ - 若存在相似代码 → 扩展/复用而非重复编写
- 若代码将被2个及以上功能使用 → 直接在中创建
modules/cores/ - 将重复出现3次及以上的逻辑提取到共享工具中
- 创建新文件后运行
npx jscpd ./src --threshold 3
Agent Workflow (MANDATORY)
Agent工作流(强制要求)
Before ANY implementation, use to spawn 3 agents:
TeamCreate- fuse-ai-pilot:explore-codebase - Analyze project structure and existing patterns
- fuse-ai-pilot:research-expert - Verify latest docs for all stack technologies
- mcp__context7__query-docs - Check integration compatibility
After implementation, run fuse-ai-pilot:sniper for validation.
在进行任何实现之前,使用生成3个Agent:
TeamCreate- fuse-ai-pilot:explore-codebase - 分析项目结构和现有模式
- fuse-ai-pilot:research-expert - 验证所有栈技术的最新文档
- mcp__context7__query-docs - 检查集成兼容性
完成实现后,运行fuse-ai-pilot:sniper进行验证。
Absolute Rules (MANDATORY)
绝对规则(强制要求)
1. Files < 100 lines
1. 文件行数<100
- Split at 90 lines - Never exceed 100
- Components < 50 lines (use composition)
- Hooks < 30 lines each
- Services < 40 lines each
- 在90行时拆分 - 绝不超过100行
- 组件<50行(使用组合方式)
- 每个钩子<30行
- 每个服务<40行
2. Modular Architecture
2. 模块化架构
See for complete structure with feature modules and cores directory.
references/architecture-patterns.md完整结构(包含功能模块和cores目录)请参见。
references/architecture-patterns.md3. JSDoc Mandatory
3. 强制使用JSDoc
typescript
/**
* Fetch user by ID from API.
*
* @param id - User unique identifier
* @returns User object or null if not found
*/
export async function getUserById(id: string): Promise<User | null>typescript
/**
* 根据ID从API获取用户信息。
*
* @param id - 用户唯一标识符
* @returns 用户对象,若未找到则返回null
*/
export async function getUserById(id: string): Promise<User | null>4. Interfaces Separated
4. 接口分离存放
text
modules/[feature]/src/interfaces/
├── user.interface.ts
├── post.interface.ts
└── api.interface.tsNEVER put interfaces in component files.
text
modules/[feature]/src/interfaces/
├── user.interface.ts
├── post.interface.ts
└── api.interface.ts绝不要将接口放在组件文件中。
SOLID Principles (Detailed Guides)
SOLID原则(详细指南)
Each SOLID principle has a dedicated reference guide:
-
- One function = one reason to change
references/single-responsibility.md- File splitting at 90 lines (components < 50, hooks < 30)
- Component composition patterns
- Split strategy
-
- Extend via composition, not modification
references/open-closed.md- Plugin architecture patterns
- Render props and slots
- Strategy patterns
-
- Contract compliance & behavioral subtyping
references/liskov-substitution.md- Interface contracts
- Swappable implementations
- Testing compliance
-
- Many focused interfaces beat one fat interface
references/interface-segregation.md- Role-based interfaces
- Props segregation
- Context splitting
-
- Depend on abstractions, not implementations
references/dependency-inversion.md- Constructor injection patterns
- Factory patterns
- Context for DI
See for overview and quick reference.
references/solid-principles.md每个SOLID原则都有专门的参考指南:
-
- 一个函数 = 一个修改理由
references/single-responsibility.md- 90行时拆分文件(组件<50行,钩子<30行)
- 组件组合模式
- 拆分策略
-
- 通过组合扩展,而非修改
references/open-closed.md- 插件架构模式
- 渲染属性和插槽
- 策略模式
-
- 契约合规性与行为子类型
references/liskov-substitution.md- 接口契约
- 可替换的实现
- 合规性测试
-
- 多个专注的接口优于一个臃肿的接口
references/interface-segregation.md- 基于角色的接口
- 属性分离
- 上下文拆分
-
- 依赖抽象,而非具体实现
references/dependency-inversion.md- 构造函数注入模式
- 工厂模式
- 用于依赖注入的上下文
概述和快速参考请参见。
references/solid-principles.mdCode Templates
代码模板
Ready-to-copy code in :
references/templates/| Template | Usage | Max Lines |
|---|---|---|
| React functional component | 50 |
| Custom hook with TanStack Query | 30 |
| Service with dependency injection | 40 |
| Zustand store with persistence | 40 |
| TypeScript interfaces | - |
| Zod validation schemas | 30 |
| Factory pattern | 40 |
| Adapter pattern | 40 |
| Custom error classes | 30 |
| Vitest + Testing Library | - |
可直接复制的代码位于:
references/templates/| 模板 | 用途 | 最大行数 |
|---|---|---|
| React函数式组件 | 50 |
| 结合TanStack Query的自定义钩子 | 30 |
| 带有依赖注入的服务 | 40 |
| 带有持久化的Zustand状态管理库 | 40 |
| TypeScript接口 | - |
| Zod验证模式 | 30 |
| 工厂模式 | 40 |
| 适配器模式 | 40 |
| 自定义错误类 | 30 |
| Vitest + Testing Library测试 | - |
Response Guidelines
响应指南
- Research first - MANDATORY: Search Context7 + Exa before ANY code
- Show complete code - Working examples, not snippets
- Explain decisions - Why this pattern over alternatives
- Include tests - Always suggest test cases
- Handle errors - Never ignore, use error boundaries
- Type everything - Full TypeScript, no
any - Document code - JSDoc for complex functions
- 先调研 - 强制要求:在编写任何代码之前先搜索Context7 + Exa
- 展示完整代码 - 可运行的示例,而非代码片段
- 解释决策 - 为何选择该模式而非其他替代方案
- 包含测试 - 始终建议测试用例
- 处理错误 - 绝不忽略,使用错误边界
- 全面类型化 - 完整的TypeScript,禁止使用
any - 文档代码 - 复杂函数需添加JSDoc
Forbidden
禁止事项
- Coding without researching docs first (ALWAYS research)
- Using outdated APIs without checking current year docs
- Files > 100 lines
- Interfaces in component files
- Business logic in components
- Class components
- Missing JSDoc on exports
- type
any - Barrel exports (index.ts re-exports)
- for data fetching (use TanStack Query or Router loaders)
useEffect - Module importing another module (except cores)
- Duplicating existing utility/helper without Grep search first
- Copy-pasting logic blocks instead of extracting shared function
- 未先调研文档就编写代码(必须始终调研)
- 使用过时API而未查看当年的文档
- 文件行数超过100
- 接口存放在组件文件中
- 组件中包含业务逻辑
- 类组件
- 导出的内容缺少JSDoc
- 使用类型
any - 桶式导出(index.ts重新导出)
- 使用进行数据获取(请使用TanStack Query或Router加载器)
useEffect - 模块导入另一个模块(cores模块除外)
- 未先搜索就重复现有的工具/辅助函数
- 复制粘贴逻辑块而非提取为共享函数