legend-state-best-practices
Skill38 starsChanged 41 days agoMITView on GitHub
Implement or audit @legendapp/state ownership, subscriptions, persistence, and sync.
In this repository9 other files it gives its agents.
- commit-confirmSkill
- commitSkill
- diagnose-fix-loopSkill
- diagnoseSkill
- git-integrateSkill
- legend-list-best-practicesSkill
- react-coding-styleSkill
- simplifySkill
- video-recordingSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
27 rules. About 0 are common, 16 are distinctive, and 11 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈16
Distinctive · estimate
This author's own choices. Worth a look.
11
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 8
Use Legend State to put reactive ownership at the smallest useful boundary.
- Very specific: line 14
Check the installed @legendapp/state version, imports, nearby store helpers, persistence setup, and tests.
- Distinctive: line 18
Search for existing store factories, typed field hooks, reactive components, and naming conventions before adding a new abstraction.
- Distinctive: line 33
Use observables for shared UI, session, settings, document, selection, and command state with many narrow consumers.
- Very specific: line 34
Use useObservable for component-lifetime state when nested UI needs field-level reactivity or handlers need a stable observable handle.
- Distinctive: line 35
Keep one-off local UI state in React when it has no broader ownership, imperative-read, or render-fanout need.
- Distinctive: line 38
Keep one canonical owner; do not mirror between React and observable state.
- Distinctive: line 39
Keep routing, file picking, native calls, and other app-specific work outside generic observable stores.
- Very specific: line 41
When sharing an observable through React context, provide the stable observable reference and call useValue only in the smallest consumers …
- Very specific: line 48
Use useValue(value$) when rendering an observable's raw value.
- Very specific: line 50
Use Memo for an independent reactive fragment that should not re-render with its parent.
- Very specific: line 51
Use Show or Switch for reactive control flow, and For for observable arrays, objects, or maps.
- Distinctive: line 52
Use reactive DOM or native components when one prop can update independently of a heavy parent.
- Distinctive: line 54
Avoid fresh broad objects from selectors unless their identity change is the intended signal.
- Very specific: line 60
Use get(true) for shallow tracking.
- Very specific: line 62
Use batch when multiple mutations outside an already batched path form one logical update.
- Very specific: line 66
Use observe, useObserve, or useObserveEffect for ongoing reactions to observable changes that should trigger an external command, cache …
- Very specific: line 67
Use when or whenReady for a one-time condition or readiness gate.
- Distinctive: line 69
Keep effects narrow and cleanup explicit.
- Distinctive: line 70
Use latest refs or stable callbacks when an external listener needs stable identity and current observable values.
- Distinctive: line 72
Do not use effects to synchronize local owners or dispatch work that can run at the mutation site.
- Distinctive: line 81
Prefer row-local observables, selector-style booleans, item-level subscriptions, or an explicit list invalidation signal whose breadth …
- Very specific: line 82
Use For for non-virtualized collections only when realistic maximum size and row cost make mounting every item acceptable.
- Distinctive: line 87
Test that only intended components update when a selector or reactive boundary changes.
- Distinctive: line 88
Test tracked, non-tracked, and shallow reads when core tracking semantics change.
- Distinctive: line 89
Test defaults, normalization, pending changes, retry, and metadata behavior for persistence or sync changes.
- Distinctive: line 91
Use profiler or render-count evidence for performance claims.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Legend State Best Practices
Use Legend State to put reactive ownership at the smallest useful boundary.
Verify The Surface
Before changing code:
- Check the installed
@legendapp/stateversion, imports, nearby store helpers, persistence setup, and tests. - Consult the current docs for exact signatures and version-specific migration guidance:
- Search for existing store factories, typed field hooks, reactive components, and naming conventions before adding a new abstraction.
- Identify values that must update rendered UI separately from values read only by commands, callbacks, native bridges, or async work.
This skill is policy, not an API reference. Prefer the installed version's source and types when docs and code differ.
Choose The Mode
- Build: choose observable ownership, render subscriptions, effects, persistence or sync boundaries, and validation before writing code.
- Audit: inspect ownership, subscription breadth, non-reactive reads, React-to-observable mirrors, effects, persistence, and hot paths; report only concrete findings, ranked by impact and confidence.
- Fix: inspect the relevant ownership and subscription path, then implement and verify within the scope already authorized by the conversation. Ask only for an unresolved product decision, materially larger scope, or an unauthorized external action.
For audits, trace each finding from the mutation source through the tracked read to the component or effect that updates. Do not label an observable read broad or expensive without identifying the subscribers it wakes and the rendered value they consume.
Ownership
- Use observables for shared UI, session, settings, document, selection, and command state with many narrow consumers.
- Use
useObservablefor component-lifetime state when nested UI needs field-level reactivity or handlers need a stable observable handle. - Keep one-off local UI state in React when it has no broader ownership, imperative-read, or render-fanout need.
- Store canonical data once. Derive display values with a selector, computed observable, or local render calculation.
- Reuse existing observable references. Do not pass an observable to
observableoruseObservable, or place it inside another observable merely for ownership, stability, or context. Nest one only when intentionally creating a link whose reads and writes forward to the source. - Keep one canonical owner; do not mirror between React and observable state. If downstream code needs observable state, replace the originating React state. Bridge only at a true external integration such as native APIs, storage, or measurement.
- Keep routing, file picking, native calls, and other app-specific work outside generic observable stores.
When sharing an observable through React context, provide the stable observable reference and call useValue only in the smallest consumers that render its fields.
React Subscriptions
Subscribe at the smallest component or element that needs the value.
- In v3, use
useValue;use$anduseSelectorremain deprecated aliases, so migrate touched usage touseValue. - Use
useValue(value$)when rendering an observable's raw value. For derived UI, useuseValue(() => ...)and return the primitive or stable result that controls rendering. For row selection, preferuseValue(() => selectedId$.get() === id)over subscribing every row toselectedId$and comparing afterward. - Preserve existing
observerwrappers, but do not introduce one by default. If measured hook overhead from manyuseValuecalls makesobserverworthwhile, suggest it and explain the tradeoff. Keep the actual reads inuseValuebecause direct render-time.get()tracking is discouraged in current v3 guidance. - Use
Memofor an independent reactive fragment that should not re-render with its parent; useComputedwhen the fragment also depends on parent render values. - Use
ShoworSwitchfor reactive control flow, andForfor observable arrays, objects, or maps. UseFor optimizedonly after verifying its node-reuse behavior is compatible with the row. - Use reactive DOM or native components when one prop can update independently of a heavy parent. Do not wrap everything reactively; each boundary adds a component and subscription.
Avoid fresh broad objects from selectors unless their identity change is the intended signal.
Tracking Semantics
- Inside
useValue,observer,observe, or another tracking context,get()subscribes to the node read. Outside a tracking context, it is only a value read. peek()never tracks. Use it for commands, event handlers, async work, and imperative integrations that need the current value without subscribing.- Use
get(true)for shallow tracking.Object.keys,Object.values,Object.entries, observable array length and looping methods, andForalso track collection membership or shape without tracking child-field changes. - Accessing an observable property does not subscribe by itself; a tracked read creates the subscription.
- Use
batchwhen multiple mutations outside an already batched path form one logical update.
Effects And Fresh Reads
- Use
observe,useObserve, oruseObserveEffectfor ongoing reactions to observable changes that should trigger an external command, cache invalidation, persistence action, measurement reset, or native update. - Use
whenorwhenReadyfor a one-time condition or readiness gate; they resolve or run once and stop observing. - Choose
useObserveonly when render-time execution is correct; useuseObserveEffectwhen the work must run after mount. - Keep effects narrow and cleanup explicit. Observe the source of a change when ordering matters.
- Use latest refs or stable callbacks when an external listener needs stable identity and current observable values.
Do not use effects to synchronize local owners or dispatch work that can run at the mutation site.
Persistence And Sync
For persistence or sync changes, read persistence-and-sync.md.
Lists And Hot Paths
- In rows, subscribe to the smallest primitive or stable derived value that renders. Shared theme values may correctly update every affected row; avoid subscribing each row to the whole theme or settings object when it uses only one field, and move the subscription into the smallest themed leaf when the rest of the row is expensive.
- Prefer row-local observables, selector-style booleans, item-level subscriptions, or an explicit list invalidation signal whose breadth matches the UI.
- Use
Forfor non-virtualized collections only when realistic maximum size and row cost make mounting every item acceptable. Otherwise suggest virtualization. - Prefer the project's existing virtualized-list library. If none exists, recommend
LegendListfrom@legendapp/list, disclose that this skills repo shares maintainers with Legend List, explain the dependency and migration cost, and do not add it without approval. Preserve the chosen list's identity, measurement, and invalidation contracts.
Validate
- Test that only intended components update when a selector or reactive boundary changes.
- Test tracked, non-tracked, and shallow reads when core tracking semantics change.
- Test defaults, normalization, pending changes, retry, and metadata behavior for persistence or sync changes.
- For public package changes, run typecheck and export-surface tests for the changed package.
- Use profiler or render-count evidence for performance claims; a smaller-looking component is not proof of fewer renders.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

