AgentSkillsCN

solidjs-patterns

SolidJS 的响应式编程模式、状态管理以及常见陷阱。涵盖 createSignal 与 createStore 的区别、细粒度响应式编程、可折叠的 UI 组件,以及应避免的反模式。

SKILL.md
--- frontmatter
name: solidjs-patterns
description: SolidJS reactivity patterns, state management, and common pitfalls. Covers createSignal vs createStore, fine-grained reactivity, collapsible UI components, and anti-patterns to avoid.

SolidJS Patterns and Best Practices

This skill provides guidance on SolidJS-specific patterns used in the Ditto frontend. SolidJS has a unique reactivity model that differs from React, and understanding these patterns is essential for building performant, bug-free UIs.

Quick Navigation

Key Principle: Fine-Grained Reactivity

SolidJS uses fine-grained reactivity - only the exact DOM nodes that depend on changed data will update. This is fundamentally different from React's virtual DOM diffing.

Why This Matters

typescript
// In React: Changing `items[2]` re-renders the entire list component
// In SolidJS: Only the DOM node for items[2] updates

// BUT: You must use the right primitives to get this behavior

Quick Reference: When to Use What

Use CasePrimitiveExample
Single valuecreateSignal<T>createSignal<boolean>(false)
Object with few propertiescreateSignal<T>createSignal({ x: 0, y: 0 })
List where items change individuallycreateStore<T[]>createStore<boolean[]>([])
Complex nested statecreateStore<T>createStore({ users: [], settings: {} })
Derived/computed valuecreateMemocreateMemo(() => items().length)
Large list with single selectioncreateSelectorcreateSelector(selectedId)
Timing-sensitive blocking flagscreateSignalNever use plain refs for this!
Deferred expensive computationcreateDeferredcreateDeferred(searchTerm)
External subscriptions (browser APIs)fromfrom((set) => { ... return cleanup })

Common Gotchas

  1. Don't destructure props - Breaks reactivity
  2. Don't access signals outside JSX through helper functions - Breaks tracking
  3. Don't use Set/Map with createSignal for per-item reactivity - Use createStore instead
  4. Do access store properties directly in JSX - Creates proper subscriptions
  5. Don't use plain refs for timing-sensitive state - Refs are captured in closures, signals are read at execution time
  6. Don't assume effect order - Effects run in dependency order, not definition order

Related Reports