Compose state hoisting

From alexandru/social-publish · View interactive version

Compose state hoisting is an AI agent skill from the GitHub repository alexandru/social-publish. 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/rememberSerial

How to use this skill

Skill file

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

Decision hints

Common pitfalls

State Hoisting Rules

Stateless vs Stateful Composables

Decide Where to Hoist

Choose the Correct Lifespan

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

Observable Types in Compose

State Callbacks (RememberObserver / RetainObserver)

Common Anti-Patterns

Output Expectations

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.