Compose State Hoisting logo

Compose State Hoisting

Community
alexandru
compose-state-hoisting

Compose state management with a strong state-hoisting preference for Kotlin Compose (Android, Multiplatform, Compose for Web). Use for refactors or new UI that needs clear state ownership, unidirectional data flow, saved state decisions, or guidance on remember/retain/rememberSaveable/rememberSerializable, and for designing stateless composables with event callbacks.

Overview

Publisheralexandru
Repositoryskills
Skill namecompose-state-hoisting
Stars
51
Forks
3
Bundled files
1
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 1 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by alexandru on GitHub. Read the source before you install it.

Installation

Install the Compose State Hoisting AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/alexandru/skills/tree/main/skills/coding/compose-state-hoisting
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/alexandru/skills.git /tmp/skills
mkdir -p .claude/skills
cp -r /tmp/skills/skills/coding/compose-state-hoisting .claude/skills/compose-state-hoisting
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Compose State Hoisting in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Compose State Hoisting on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Compose State Hoisting is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

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.

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Compose State Hoisting AI skill do?

Compose state management with a strong state-hoisting preference for Kotlin Compose (Android, Multiplatform, Compose for Web). Use for refactors or new UI that needs clear state ownership, unidirectional data flow, saved state decisions, or guidance on remember/retain/rememberSaveable/rememberSerializable, and for designing stateless composables with event callbacks.

Why use Compose State Hoisting on TypingMind?

Because you install it once and use it with any model. Compose State Hoisting is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Compose State Hoisting in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/alexandru/skills/tree/main/skills/coding/compose-state-hoisting. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Compose State Hoisting?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Compose State Hoisting?

As many as you like. As long as a model supports skills, you can use Compose State Hoisting with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Compose State Hoisting AI skill free?

It is published on GitHub by alexandru. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇