Agent Skillsstagewise-io/stagewise › karton-best-practices

karton-best-practices

GitHub

提供Karton状态库的React最佳实践,涵盖useKartonState等Hook的性能优化、选择器模式、避免重渲染及Immer结构共享原理。

.agents/skills/karton-best-practices/SKILL.md stagewise-io/stagewise

Trigger Scenarios

编写使用Karton Hooks的React组件 审查涉及Karton状态的代码性能 解决React组件因状态更新导致的异常重渲染问题

Install

npx skills add stagewise-io/stagewise --skill karton-best-practices -g -y
More Options

Non-standard path

npx skills add https://github.com/stagewise-io/stagewise/tree/main/.agents/skills/karton-best-practices -g -y

Use without installing

npx skills use stagewise-io/stagewise@karton-best-practices

指定 Agent (Claude Code)

npx skills add stagewise-io/stagewise --skill karton-best-practices -a claude-code -g -y

安装 repo 全部 skill

npx skills add stagewise-io/stagewise --all -g -y

预览 repo 内 skill

npx skills add stagewise-io/stagewise --list

SKILL.md

Frontmatter
{
    "name": "karton-best-practices",
    "description": "Performance-focused guidelines for writing React code that consumes Karton state. Use when creating or reviewing components that use useKartonState, useKartonProcedure, useComparingSelector, or any Karton React hooks. Covers selector patterns, re-render prevention, structural sharing, and edge cases."
}

Karton React Best Practices

Performance guidelines for React components consuming Karton state. Karton uses Immer internally — understanding its structural sharing model is essential for writing efficient selectors.

Core Mental Model

Karton state is deeply frozen (Immer freeze(state, true)). Updates use produce() which creates new references only for changed paths — unchanged subtrees keep the same reference. useSyncExternalStore uses Object.is (reference equality) to decide re-renders.

The golden rule: Your selector's return value must have a stable reference when nothing relevant changed. If it doesn't, wrap it with useComparingSelector.

Quick Rules

Selectors

// GOOD: Select a primitive — stable when unchanged
const isWorking = useKartonState((s) => s.agents.instances[id]?.state.isWorking ?? false);

// GOOD: Select a frozen subtree — Immer preserves the reference
const agent = useKartonState((s) => s.agents.instances[id]);

// BAD: Creates a new object every call — always re-renders
const data = useKartonState((s) => ({
  name: s.agents.instances[id]?.name,
  status: s.agents.instances[id]?.state.isWorking,
}));

// FIX: Wrap with useComparingSelector (default: shallow equality)
const data = useKartonState(
  useComparingSelector((s) => ({
    name: s.agents.instances[id]?.name,
    status: s.agents.instances[id]?.state.isWorking,
  })),
);

// BAD: Object.keys/values/entries always creates a new array
const ids = useKartonState((s) => Object.keys(s.agents.instances));

// FIX: shallow comparison handles arrays element-by-element
const ids = useKartonState(
  useComparingSelector((s) => Object.keys(s.agents.instances)),
);

When to Use useComparingSelector

Selector returns Needs useComparingSelector?
Primitive (boolean, string, number) No
Existing frozen subtree reference No
New object/array created in selector Yes (use shallow)
Deeply nested new structures Yes (use deep)

Procedures

// GOOD: Procedures are stable proxies — select once, use freely
const createAgent = useKartonProcedure((p) => p.agents.create);

// GOOD: Store in ref for use in callbacks to prevent effect re-triggers
const createAgentRef = useRef(createAgent);
createAgentRef.current = createAgent;

Derived State

// GOOD: useMemo to compute derived values from selected state
const agent = useKartonState((s) => s.agents.instances[id]);
const displayName = useMemo(() => agent?.name ?? 'Unnamed', [agent]);

// BAD: Expensive computation inside selector (runs on every state change)
const sorted = useKartonState((s) =>
  Object.values(s.agents.instances).sort((a, b) => a.name.localeCompare(b.name)),
);

// FIX: Select raw data, compute outside
const instances = useKartonState(
  useComparingSelector((s) => s.agents.instances),
);
const sorted = useMemo(
  () => Object.values(instances).sort((a, b) => a.name.localeCompare(b.name)),
  [instances],
);

Key Pitfalls

  1. Every useKartonState call runs its selector on every state change — keep selectors cheap
  2. Selectors that create new objects/arrays always cause re-renders unless wrapped with useComparingSelector
  3. useComparingSelector with deep is expensive — prefer shallow or restructure state
  4. Selecting the full state (useKartonState() with no selector) re-renders on any change
  5. Inline selector functions recreate on every render — this is fine because useSyncExternalStore compares snapshot values, not function references
  6. Connection changes (onOpen/onClose) trigger all listeners — components using useKartonState may re-render briefly on reconnect even if state didn't change

References

For detailed analysis, see:

  • references/state-update-model.md — How Immer structural sharing affects object references
  • references/selector-patterns.md — Comprehensive selector patterns with examples
  • references/re-render-prevention.md — useComparingSelector internals and memoization strategies
  • references/edge-cases.md — Arrays, Maps, conditional selection, and reconnection behavior

Version History

  • 45a1544 Current 2026-07-24 21:12

Same Skill Collection

.agents/skills/add-llm-model/SKILL.md
.agents/skills/caveman/SKILL.md
.agents/skills/create-skill/SKILL.md
.agents/skills/history-compression/SKILL.md
.agents/skills/stage-ui-design-system/SKILL.md
.agents/skills/vercel-react-best-practices/SKILL.md
apps/browser/bundled/plugins/figma/SKILL.md
apps/browser/bundled/plugins/github/SKILL.md
apps/browser/bundled/plugins/javascript-sandbox/SKILL.md
apps/browser/bundled/plugins/mini-apps/SKILL.md
apps/browser/bundled/plugins/posthog/SKILL.md
apps/browser/bundled/plugins/remotion/SKILL.md
apps/browser/bundled/plugins/supabase/SKILL.md
apps/browser/bundled/plugins/vercel/SKILL.md
apps/browser/bundled/skills/debug/SKILL.md
apps/browser/bundled/skills/learn-skill/SKILL.md
apps/browser/bundled/skills/plan/SKILL.md
apps/browser/bundled/skills/preview/SKILL.md
apps/browser/bundled/skills/watch/SKILL.md
.agents/skills/copywriting/SKILL.md
.agents/skills/prompt-optimization/SKILL.md
apps/browser/bundled/skills/implement/SKILL.md

Metadata

Files
0
Version
18ac8a2
Hash
c5987302
Indexed
2026-07-24 21:12

Главная - Вики-сайт
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-20 14:14
浙ICP备14020137号-1 $Гость$