Token导航 LogoToken导航TokenDH.com
开发需要联网github未标认证来源可访问许可证需确认审计通过

compose-state-hoisting编写状态提升

Agent Skill

compose-state-hoisting 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要围绕仓库状态、代码变更或协作事项进行整理时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

212

周安装

9

GitHub Stars

36

下载量

74
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:compose-state-hoisting(编写状态提升)
来源仓库:https://github.com/alexandru/skills
仓库路径:skills/compose-state-hoisting
安装命令:
npx skills add https://github.com/alexandru/skills --skill compose-state-hoisting
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/alexandru/skills --skill compose-state-hoisting

简介

compose-state-hoisting 应用状态提升和单向数据流优化 Compose UI。

  • 适用于需要选择正确状态所有者、寿命和存储策略的场景。
  • 支持 lowest common ancestor、ViewModel 等多种状态管理方案。
  • 根据生存期需求选择 remember/retain/rememberSaveable 等 API。
  • 使用时需确保 UI composables 尽可能无状态,事件通过回调向上传递。

SKILL.md

Compose State Hoisting

Overview

Apply state hoisting and unidirectional data flow to Compose UIs, choosing the right state owner, lifespan, and saving strategy.

For condensed reference and examples, see references/compose-state-guidance.md.

Workflow

  1. Identify the state and the logic that reads/writes it.
  2. Decide the state owner (lowest common ancestor; or a plain state holder class, or a screen-level state holder like Android ViewModel for complex UI/business logic).
  3. Choose the lifespan API (remember/retain/rememberSaveable/rememberSerializable) based on how long it must survive.
  4. Make UI composables stateless: pass value and event callbacks; state goes down, events go up.
  5. Decide what must be saved and how (rememberSaveable, SavedStateHandle, or platform storage).
  6. Verify that state and callbacks are not duplicated or leaked.

Rules of thumb

  • Hoist state to the lowest common ancestor of all readers/writers.
  • Keep composables stateless: state down, events up.
  • Pick the lifespan API explicitly; default to remember only for ephemeral UI state.
  • Save minimal data and rehydrate anything larger or derived.

Decision hints

  • If multiple parts of the UI must read/write the same state, hoist it.
  • If state survives config changes or process death, choose retain or rememberSaveable and document why.
  • For business logic, use a screen-level state holder; do not pass it down the tree.

Common pitfalls

  • Duplicated state owners causing divergence.
  • Over-saving large objects in rememberSaveable/SavedStateHandle.
  • Mixing remember and retain for the same object.

State Hoisting Rules

  • Hoist state to the lowest common ancestor of all composables that read and write it; keep it as close to consumers as possible.
  • If multiple states change from the same events, hoist them together.
  • Over-hoisting (e.g., hoisting to screen level when a subtree would suffice) is acceptable and safer than under-hoisting; under-hoisting breaks unidirectional flow and creates duplicate sources of truth. Over-hoisting may trigger more recompositions or lose state on navigation.
  • Prefer exposing immutable state plus event callbacks from the state owner.

Stateless vs Stateful Composables

  • Provide a stateless API: value: T and onValueChange: (T) -> Unit (or more specific event lambdas).
  • Keep state internal only if no other composable needs to read or change it and the UI logic is simple.
  • Offer both stateful and stateless variants when useful; the stateless version is the reusable/testable one.

Decide Where to Hoist

  • UI element state + simple UI logic: keep internal or hoist within the UI subtree.
  • Complex UI logic: move state and UI logic into a plain state holder class scoped to the Composition.
  • Business logic or screen UI state: hoist to a screen-level state holder (Android: ViewModel; Multiplatform: platform-specific or library solutions like Circuit, Molecule). Do not pass screen-level state holders down the tree; inject at the screen level and pass state/events instead.
  • Deep prop drilling: for state needed by many distant descendants (theme, user session), consider CompositionLocal to avoid passing parameters through many layers. Use sparingly; prefer explicit parameter passing when practical.

Choose the Correct Lifespan

  • remember: survives recomposition only; same instance. Use for composition-scoped objects and small internal UI state. Do not use for user input that must be preserved.
  • retain: survives recomposition + window/configuration changes (Android: activity recreation), not process death. Use for non-serializable objects (players, caches, flows, lambdas). Do not retain platform-specific lifecycle objects (Android: Activity, View, Fragment, ViewModel, Context, Lifecycle). Do not retain objects that were already created with remember by the caller—retain and remember are mutually exclusive for the same object.
  • rememberSaveable / rememberSerializable: survives recomposition + configuration changes + process death (Android) by saving to the platform's saved state mechanism (Android: Bundle; other platforms may vary). Use for user input or UI state that cannot be reloaded from another source. Restored objects are equal but not the same instance.

Remember Keys: Control when state resets by passing keys to remember(key1, key2) {}. State recreates when any key changes. Omit keys only when state should survive all recompositions.

Saving UI State

  • Use rememberSaveable for UI state hoisted in composables or plain state holders; save only minimal, small data.
  • Saved state storage is limited (Android Bundle: ~1MB); do not store large objects or lists. Store IDs/keys and rehydrate from data/persistent storage.
  • Android: Use SavedStateHandle in a ViewModel for UI element state that must survive process death; keep it small and session-scoped (not persistent app data).
  • Do not save full screen UI state; rebuild it from the data layer on restoration.

Observable Types in Compose

  • Convert observable types to State<T> before reading in composables.
  • Flow: use collectAsState (platform-agnostic, always collects) or collectAsStateWithLifecycle (Android only, lifecycle-aware, pauses collection when UI is not visible).
  • LiveData (Android): use observeAsState.
  • For custom observables, create a State<T> via produceState.

State Callbacks (RememberObserver / RetainObserver)

  • Run initialization side-effects in onRemembered / onRetained, not in constructors or remember/retain lambdas.
  • Always cancel work in onForgotten / onRetired; handle onAbandoned for canceled compositions.
  • Keep implementations private; expose safe factory functions like rememberX() to avoid misuse.
  • Do not remember the same object twice; do not pass parameters that are already wrapped in State<T> to another remember call—this creates unnecessary nested observability.

Common Anti-Patterns

  • Storing mutable collections or mutable data classes directly as state; prefer immutable containers wrapped in State or use snapshot state collections (mutableStateListOf, mutableStateMapOf).
  • Duplicating state in multiple owners instead of hoisting to a single source of truth.
  • Mixing remember and retain for the same object; remembering/retaining objects with mismatched lifespans.
  • Saving large or complex objects in rememberSaveable/SavedStateHandle (Android); save IDs and rehydrate instead.
  • Computing derived values inside composables without derivedStateOf, causing unnecessary recompositions.

Output Expectations

  • Favor stateless composables with value + callbacks.
  • Prefer lowest common ancestor hoisting.
  • Choose lifecycle APIs intentionally; call out saving strategy explicitly.
  • Keep state minimal, immutable, and observable.

References

  • Load references/compose-state-guidance.md for decision trees, tables, and code examples.

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

32.53%
按下载量换算24

Claude

31.19%
按下载量换算23

Cursor

17.75%
按下载量换算13

Gemini CLI

9.73%
按下载量换算7

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills