solidjs-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSolidJS Patterns and Best Practices
SolidJS 模式与最佳实践
Comprehensive correctness and performance guide for SolidJS and SolidStart applications, maintained by OmniAura. Contains 49 rules across 9 categories, prioritized by impact to guide automated refactoring and code generation. Built from production experience migrating from React to SolidJS.
由OmniAura维护的SolidJS与SolidStart应用程序全面性正确性及性能指南。包含9个类别下的49条规则,按影响优先级排序,可指导自动化重构与代码生成。基于从React迁移至SolidJS的生产环境经验构建。
When to Apply
适用场景
Reference these guidelines when:
- Writing new SolidJS components or SolidStart pages
- Migrating React code to SolidJS
- Implementing data fetching (Solid Query, createAsync, createResource)
- Working with SolidStart server functions ("use server")
- Reviewing code for reactivity correctness issues
- Optimizing bundle size or rendering performance
- Setting up state management (signals, stores, context)
- Writing tests for reactive code
在以下场景中参考本指南:
- 编写新的SolidJS组件或SolidStart页面
- 将React代码迁移至SolidJS
- 实现数据获取(Solid Query、createAsync、createResource)
- 处理SolidStart服务器函数("use server")
- 审查代码中的响应式正确性问题
- 优化包体积或渲染性能
- 搭建状态管理(signals、stores、context)
- 为响应式代码编写测试
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Reactivity Correctness | CRITICAL | |
| 2 | Data Fetching & Server | CRITICAL | |
| 3 | Component Patterns | HIGH | |
| 4 | State Management | HIGH | |
| 5 | Rendering & Control Flow | MEDIUM-HIGH | |
| 6 | SolidStart Patterns | MEDIUM-HIGH | |
| 7 | Performance Optimization | MEDIUM | |
| 8 | Testing | LOW-MEDIUM | |
| 9 | External Interop | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 响应式正确性 | 严重(CRITICAL) | |
| 2 | 数据获取与服务器端 | 严重(CRITICAL) | |
| 3 | 组件模式 | 高(HIGH) | |
| 4 | 状态管理 | 高(HIGH) | |
| 5 | 渲染与控制流 | 中高(MEDIUM-HIGH) | |
| 6 | SolidStart 模式 | 中高(MEDIUM-HIGH) | |
| 7 | 性能优化 | 中(MEDIUM) | |
| 8 | 测试 | 中低(LOW-MEDIUM) | |
| 9 | 外部互操作 | 低(LOW) | |
Quick Reference
速查指南
1. Reactivity Correctness (CRITICAL)
1. 响应式正确性(严重)
- - Never destructure props — breaks reactivity
reactivity-no-destructure-props - - Access signals/stores directly in JSX, not via helpers
reactivity-no-helper-access - - Use store instead of Set/Map with signal for collections
reactivity-no-set-map-signal - - Use splitProps instead of rest spread
reactivity-no-rest-spread - - Use signals for timing-sensitive state, not plain refs
reactivity-signals-not-refs - - Never mutate store values directly
reactivity-no-direct-mutation - - Don't assume effect execution order
reactivity-no-effect-order - - Always clean up effects with onCleanup
reactivity-cleanup-effects - - Use on() to break circular dependencies
reactivity-on-explicit-tracking - - Use createDeferred for expensive computations on rapid input
reactivity-create-deferred - - Use createReaction to separate tracking from side effects
reactivity-create-reaction - - Don't capture signals in closures outside reactive context
reactivity-no-signal-capture
- - 切勿解构props——这会破坏响应式特性
reactivity-no-destructure-props - - 在JSX中直接访问signals/stores,而非通过辅助函数
reactivity-no-helper-access - - 对于集合类型,使用store而非结合signal的Set/Map
reactivity-no-set-map-signal - - 使用splitProps替代剩余扩展运算符
reactivity-no-rest-spread - - 对时间敏感的状态使用signals,而非普通refs
reactivity-signals-not-refs - - 切勿直接修改store的值
reactivity-no-direct-mutation - - 不要假设effect的执行顺序
reactivity-no-effect-order - - 始终使用onCleanup清理effects
reactivity-cleanup-effects - - 使用on()打破循环依赖
reactivity-on-explicit-tracking - - 对快速输入场景下的昂贵计算使用createDeferred
reactivity-create-deferred - - 使用createReaction将跟踪与副作用分离
reactivity-create-reaction - - 不要在响应式上下文之外的闭包中捕获signals
reactivity-no-signal-capture
2. Data Fetching & Server (CRITICAL)
2. 数据获取与服务器端(严重)
- - Never destructure Solid Query results
data-no-destructure-query - - Don't create query waterfalls
data-parallel-queries - - Guard .data access to prevent unwanted Suspense
data-guard-suspense - - Include all dependencies in query keys
data-include-all-query-keys - - Wrap query options in arrow function
data-query-options-function - - Invalidate queries after mutations
data-mutation-invalidation
- - 切勿解构Solid Query的结果
data-no-destructure-query - - 不要创建查询瀑布
data-parallel-queries - - 对.data的访问进行防护,以避免不必要的Suspense
data-guard-suspense - - 在查询键中包含所有依赖项
data-include-all-query-keys - - 将查询选项包裹在箭头函数中
data-query-options-function - - 执行mutation后使查询失效
data-mutation-invalidation
3. Component Patterns (HIGH)
3. 组件模式(高)
- - Don't return early before reactive primitives
component-no-early-return - - Use mergeProps for reactive default values
component-merge-props - - Use splitProps to separate and forward props
component-split-props
- - 不要在响应式原语之前提前返回
component-no-early-return - - 使用mergeProps处理响应式默认值
component-merge-props - - 使用splitProps分离并转发props
component-split-props
4. State Management (HIGH)
4. 状态管理(高)
- - Choose signal vs store based on update granularity
state-signal-vs-store - - Use typed context with store for global state
state-context-pattern - - Use reconcile for fine-grained async updates
state-reconcile-async - - Use produce() for complex store mutations
state-produce-complex-mutations - - Use createStore for multi-field form state
state-form-store
- - 根据更新粒度选择signal或store
state-signal-vs-store - - 结合类型化context与store实现全局状态管理
state-context-pattern - - 使用reconcile处理细粒度异步更新
state-reconcile-async - - 使用produce()处理复杂的store变更
state-produce-complex-mutations - - 使用createStore处理多字段表单状态
state-form-store
5. Rendering & Control Flow (MEDIUM-HIGH)
5. 渲染与控制流(中高)
- - Use
rendering-use-for-not-mapinstead of .map() for lists<For> - - Use
rendering-use-show-not-ternaryinstead of JSX conditionals<Show> - - Place Suspense inside conditionals (LazyShow pattern)
rendering-suspense-inside-show - - Use Switch/Match for multi-branch conditionals
rendering-use-switch-match - - Wrap risky components in ErrorBoundary
rendering-error-boundary - - Choose between For and Index based on what changes
rendering-index-vs-for
- - 对于列表渲染,使用
rendering-use-for-not-map而非.map()<For> - - 使用
rendering-use-show-not-ternary而非JSX条件运算符<Show> - - 将Suspense置于条件语句内部(LazyShow模式)
rendering-suspense-inside-show - - 使用Switch/Match处理多分支条件
rendering-use-switch-match - - 用ErrorBoundary包裹风险组件
rendering-error-boundary - - 根据变更内容选择For或Index
rendering-index-vs-for
6. SolidStart Patterns (MEDIUM-HIGH)
6. SolidStart 模式(中高)
- - Always validate "use server" function inputs
start-use-server-validation - - Use createAsync + query() for data loading
start-createasync-not-resource - - Always define route preload functions
start-route-preloading
- - 始终对"use server"函数的输入进行验证
start-use-server-validation - - 使用createAsync + query()处理数据加载
start-createasync-not-resource - - 始终定义路由预加载函数
start-route-preloading
7. Performance Optimization (MEDIUM)
7. 性能优化(中)
- - Lazy load heavy components
perf-lazy-load-heavy-components - - Use createSelector for single-selection in large lists
perf-create-selector - - Use createMemo for expensive derived computations
perf-memo-expensive - - Use lazy() for route-level code splitting
perf-route-code-splitting - - Use skeleton components for Suspense fallbacks
perf-skeleton-suspense - - Use useTransition for non-blocking async updates
perf-use-transition - - Use createRenderEffect for synchronous DOM measurements
perf-render-effect
- - 懒加载重型组件
perf-lazy-load-heavy-components - - 在大型列表中使用createSelector实现单选
perf-create-selector - - 使用createMemo处理昂贵的派生计算
perf-memo-expensive - - 使用lazy()实现路由级代码分割
perf-route-code-splitting - - 为Suspense回退状态使用骨架屏组件
perf-skeleton-suspense - - 使用useTransition处理非阻塞异步更新
perf-use-transition - - 使用createRenderEffect处理同步DOM测量
perf-render-effect
8. Testing (LOW-MEDIUM)
8. 测试(中低)
- - Wrap reactive test code in createRoot
testing-createroot - - Use render() and Testing Library for component tests
testing-render-components - - Use renderHook for testing custom hooks
testing-render-hook - - Use waitFor for async component testing
testing-async-waitfor - - Use vi.hoisted with vi.mock for proper mock hoisting
testing-mock-hoisted
- - 将响应式测试代码包裹在createRoot中
testing-createroot - - 使用render()与Testing Library进行组件测试
testing-render-components - - 使用renderHook测试自定义hooks
testing-render-hook - - 使用waitFor进行异步组件测试
testing-async-waitfor - - 结合vi.hoisted与vi.mock实现正确的mock提升
testing-mock-hoisted
9. External Interop (LOW)
9. 外部互操作(低)
- - Use from() to bridge browser APIs into reactive signals
interop-from-browser-apis - - Use observable() to expose signals to external libraries
interop-observable-export
- - 使用from()将浏览器API桥接至响应式signals
interop-from-browser-apis - - 使用observable()向外部库暴露signals
interop-observable-export
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/reactivity-no-destructure-props.md
rules/data-parallel-queries.md
rules/start-use-server-validation.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Impact rating and tags
- Additional context and references
阅读单个规则文件以获取详细说明与代码示例:
rules/reactivity-no-destructure-props.md
rules/data-parallel-queries.md
rules/start-use-server-validation.md每个规则文件包含:
- 规则重要性的简要说明
- 错误代码示例及解释
- 正确代码示例及解释
- 影响评级与标签
- 额外上下文与参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md包含所有规则扩展说明的完整指南:
AGENTS.mdLegacy Files
遗留文件
The following files contain additional detailed patterns and are being migrated into individual rules:
- - Reactivity fundamentals
reactivity.md - - on() and untrack() patterns
explicit-tracking.md - - State management patterns
state-patterns.md - - Common anti-patterns
anti-patterns.md - - Secondary primitives
advanced-primitives.md - - from() and observable() patterns
external-interop.md - - Solid Query patterns
solid-query.md - - Performance optimization
performance.md - - Testing patterns
testing.md
以下文件包含更多详细模式,正逐步迁移至单个规则中:
- - 响应式编程基础
reactivity.md - - on()与untrack()模式
explicit-tracking.md - - 状态管理模式
state-patterns.md - - 常见反模式
anti-patterns.md - - 二级原语
advanced-primitives.md - - from()与observable()模式
external-interop.md - - Solid Query模式
solid-query.md - - 性能优化
performance.md - - 测试模式
testing.md