JetBrains/intellij-community / .agents/skills/compose-ui
compose-ui
VerifiedWrite or audit Compose Desktop UI state, effects, and boundaries.
audit/references/effects/derived-state-of-guide.md
View on GitHub# derivedStateOf Guide
`derivedStateOf` creates a new `State` that derives from one or more upstream states. Its purpose
is to prevent recompositions when the upstream changes but the derived value does not.
## When to use
Use `derivedStateOf` when:
- The derivation **filters** or **collapses** upstream changes.
- Downstream composables should not recompose on every upstream tick.
## When NOT to use
Do not use `derivedStateOf` when:
- The derivation is trivial and never filters (e.g. `items.size`).
- The derived value always changes when upstream changes.
- You only read the value once and do not observe it.
## Examples
### RIGHT — filters upstream changes
```kotlin
val isScrolled by remember {
derivedStateOf { scrollState.value > 0 }
}
```
`scrollState.value` changes on every scroll pixel. `isScrolled` only changes once — when the
threshold is crossed. Consumers of `isScrolled` recompose once instead of on every scroll event.
### RIGHT — collapses a collection
```kotlin
val hasErrors by remember {
derivedStateOf { items.any { it.isError } }
}
```
`items` may change frequently, but `hasErrors` only changes when an error appears or disappears.
### WRONG — trivial transformation
```kotlin
// Overhead with no benefit
val count by remember { derivedStateOf { items.size } }
```
`items.size` changes whenever `items` changes. Just read `items.size` directly.
### WRONG — always changes
```kotlin
// Overhead with no benefit
val doubled by remember { derivedStateOf { counter.value * 2 } }
```
`doubled` changes on every `counter` change. Just read `counter.value * 2` directly.
## Performance note
`derivedStateOf` adds a small overhead (extra state object, extra observer). The benefit is
negative if the derived value changes as often as the upstream. Use it only when the derivation
reduces recomposition frequency.
## Common mistake: writing inside derivedStateOf
```kotlin
// WRONG — side effects inside derivedStateOf
val filtered by remember {
derivedStateOf {
if (items.isEmpty()) showEmpty.value = true
items.filter { it.visible }
}
}
```
`derivedStateOf` must be pure. Move side effects to `LaunchedEffect` or `SideEffect`.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |