ronjunevaldoz/kmp-agent-skills/kmp-compose-state-hoisting
Claude Code and agent skill by ronjunevaldoz/kmp-agent-skills
- community
Checking that the SKILL.md file is still in the repository.
State hoisting in Compose Multiplatform — the pattern of moving state up to the lowest common ancestor that needs it. Covers: stateful vs stateless composables, the controlled component pattern (value + onValueChange), the hoist-until-shared rule, UI state vs business state distinction, when to stop hoisting, and the common mistakes of over-hoisting (everything in ViewModel) and under-hoisting (buried state that can't be tested). Zero new dependencies.
Install
npx vskill@latest install ronjunevaldoz/kmp-agent-skills/kmp-compose-state-hoistingSKILL.md
The same SKILL.md was published earlier as ronjunevaldoz/kmp-agent-skills/kmp-compose-state-hoisting.
When to Use This Skill
Use when you need to:
- Decide where state should live in a Compose component tree
- Make a composable testable and previewable by removing internal state
- Share state between sibling composables
- Understand why a component is hard to test (state is buried)
- Explain or implement "controlled" vs "uncontrolled" composable patterns
Trigger keywords: state hoisting, lift state, hoist state, stateful composable, stateless composable, controlled input, value onValueChange, where does state go, single source of truth, state sharing, Compose state management.
Freshness rule: Compose state management guidance tracks CMP releases — recheck the JetBrains CMP docs before upgrading or copying patterns into a new project.
Recommendation First
Default to hoisting state to the lowest common ancestor of all consumers.
Why:
- hoisted state makes a composable stateless and previewable with fixed input
- a stateless composable is trivially unit-testable — no ViewModel or Compose rule needed
- shared state belongs at the level where siblings can both read and write it, not duplicated
Keep state internal (unhoisted) only when it is truly ephemeral and no other composable in the tree will ever need it (e.g., a tooltip open flag on a local button).
The Core Rule
Hoist state to the lowest ancestor that all consumers share.
That's it. Everything else is application of this rule.
If only one composable reads and writes a value, state stays there. If two siblings need it, it moves to their parent. If the whole screen needs it, it belongs in a ViewModel.
Stateful vs Stateless Composables
Stateful — owns its own state internally:
Stateless — caller provides state and a callback to change it:
The stateless version is:
- Testable — pass any count value, verify the display
- Previewable —
CounterButton(count = 42, onIncrement = {}) - Shareable — the count can be read by sibling composables
- Controllable — a parent can reset, cap, or react to the count
The stateful version is convenient for truly self-contained UI (a toggle with no external observers), but is a dead end for sharing or testing.
The Controlled Component Pattern
Kotlin/Compose's equivalent of React's controlled input: the component receives the current value and a callback to request a change, but never owns the state.
The parent owns the value:
Why this matters: with the controlled pattern, the parent can:
- Validate:
onValueChange = { if (it.length <= 50) email = it } - Transform:
onValueChange = { email = it.lowercase() } - React: disable the submit button while email is invalid
- Reset: clear both fields after a failed login attempt
The Hoist-Until-Shared Rule in Practice
Full content: references/hoist-until-shared-rule.md.
derivedStateOf — Memoize Derived Compose State
Use derivedStateOf when a computation reads one or more State values and you only want it to re-run (and trigger recomposition) when the result changes, not every time any input state ticks.
When to use `derivedStateOf`:
| Situation | Use | |
|---|---|---|
Derived value changes less often than inputs (e.g., isValid from a text field) | derivedStateOf | |
| Derived value changes at the same rate as inputs | Plain expression — derivedStateOf adds overhead for no gain | |
| Multiple unrelated states feed one derived value | derivedStateOf — avoids redundant recompositions | |
| The expression is expensive (sort, filter a list) | derivedStateOf — caches until inputs change |
Rule: wrap derivedStateOf in remember — otherwise a new DerivedState object is created on every recomposition and the memoization is lost.
@Stable and @Immutable — Compose Stability Annotations
The Compose compiler infers whether a type is stable (reads are deterministic and Compose is notified of any change) or unstable (Compose can't prove this). Composables whose parameters are all stable can be skipped when the parent recomposes and none of the parameters changed.
data class parameters that contain only primitives, String, and other stable types are inferred as stable automatically. Classes with mutable fields, interfaces, or non-Compose-aware collections are inferred as unstable — which disables skipping for every composable that receives them.
Add annotations to opt in explicitly:
| Annotation | Contract | Use when | |
|---|---|---|---|
@Immutable | All public fields are deeply immutable | data class with only val of primitive / String / @Immutable types | |
@Stable | Reads are stable; Compose notified of any change | Fields include List, Map, or other types Compose can't prove are stable | |
| Neither | Compose conservatively marks unstable | When you haven't audited stability yet — add annotations once you know the type is safe |
Where to apply:
- MVI
Statedata classes (seekmp-mvi) — the most impactful place - Design system types passed down through deep composable trees
- ViewModel-owned UI model classes that appear as composable parameters
Do NOT annotate types that genuinely mutate without notifying Compose — the annotation is a contract, and breaking it produces hard-to-debug phantom recompositions.
When to Stop Hoisting
Not everything belongs in a ViewModel. Over-hoisting creates bloated ViewModels full of UI-only state that has nothing to do with business logic.
Keep in local `remember`:
- Dropdown open/closed
- Tooltip visible
- Focus state
- Animation target values
- Temporary input before validation
- Scroll position (use
rememberScrollState())
Move to ViewModel:
- Any async data load result
- Form values that persist across navigation
- State shared with another screen
- State derived from repository data
- State that affects what API calls are made
Providing a Stateful Convenience Wrapper
For components used in both controlled (hoisted) and standalone contexts, provide both:
Prefer the stateless version in design systems — leave the choice to the caller.
State Hoisting and the Screen/Content Split
Combining state hoisting with the Screen/Content split (from kmp-mvi) gives you fully testable leaf composables:
This is the end result of applying both state hoisting and the MVI pattern together: the Screen composable holds nothing, the ViewModel holds business state, and the Content composable is a pure function of state.
Common Mistakes
Full content: references/common-mistakes.md.
Verification
- Stateless composable previews render correctly with any state value
- Sibling composables sharing hoisted state stay in sync when either changes
- ViewModel state change triggers recomposition of
Contentcomposable - Inline
rememberstate (dropdown, tooltip) does NOT persist to ViewModel - Unit test: instantiate
Contentwith fixed state, assert rendered output without a ViewModel
Testing
Full content: references/testing.md.
Common Anti-Patterns
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/kmp-compose-state-hoisting/SKILL.md
Popularity
- 7-day trend score
- 5
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/ronjunevaldoz/kmp-agent-skills/kmp-compose-state-hoisting)