compose-state-authoring

chrisbanes/skills

Состояние в Jetpack Compose: remember, mutableStateOf и @ReadOnlyComposable.

Звёзды1 162
Авторchrisbanes

Установка

SKILL.md

Compose state authoring

Not every remember { … } belongs here. This skill covers local UI state (remember { mutableStateOf(…) }, mutableStateListOf / mutableStateMapOf) and @ReadOnlyComposable. Other remembered APIs live in focused skills:

  • rememberCoroutineScope / rememberUpdatedState → compose-side-effects
  • rememberLazyListState / rememberScrollState used for frame-rate reads → compose-state-deferred-reads
  • Focus navigation, focus state, FocusRequester ownership, behavior → compose-focus-navigation

Core principle

A @Composable is a function the runtime re-runs whenever its inputs change. Writing local state correctly comes down to two questions:

  1. Mutable local state — does my var survive recomposition and trigger it? If not, it silently resets on every recompose and writes are invisible.
  2. What kind of composable is this? — do I mutate composition (place layout nodes, allocate slots, remember) or only read it? If only read, @ReadOnlyComposable lets the runtime skip work.

Get either wrong and the symptoms are subtle: state that vanishes or optimizations that don't apply.

When to use this skill

You're writing or reviewing Compose code and you see any of these:

  • var x = … inside a @Composable fun or any composable lambda (Column { var x = … })
  • A @Composable fun (or @Composable get() property accessor) whose body never lays anything out
  • @ReadOnlyComposable on a function that calls Text, Box, Column, remember, …
  • A composable whose visible state mysteriously resets on rotation, theme change, or recomposition

1. var in a composable must be State-backed

Recomposition re-executes the composable from the top. A local var is re-initialized on every pass — last recompose's value is gone, and writing to it doesn't tell the runtime to recompose.

// ❌ BAD — counter resets on every recomposition; clicks never update the UI
@Composable
fun Counter() {
    var count = 0
    Button(onClick = { count++ }) { Text("$count") }
}

// ❌ ALSO BAD — same rule applies inside composable content lambdas
@Composable
fun Wrapper() {
    Row {
        var count = 0         // Row's content lambda is @Composable too
        // …
    }
}
// ✅ GOOD — `remember` survives recomposition, `mutableStateOf` triggers it
@Composable
fun Counter() {
    var count by remember { mutableStateOf(0) }
    Button(onClick = { count++ }) { Text("$count") }
}

Two pieces and both matter:

  • remember { … } — survives recomposition. Without it the value is re-created each time.
  • mutableStateOf(…) — triggers recomposition. Without it, mutations are invisible to the runtime.

For collections, prefer mutableStateListOf / mutableStateMapOf (also remember-ed). They emit Snapshot reads on every read and Snapshot writes on every mutation. A remember { mutableStateOf(mutableListOf<X>()) } followed by list.add(x) will not recompose, because MutableList.add doesn't go through the State setter — you'd have to replace the value (state = state + x).

Back-writing snapshot state during composition

Back-writing means writing observable state in a phase that triggers invalidation of an earlier (or the current) phase. Mutating mutableState* from the composable body back-writes into the same composition pass and schedules another. Do not rebuild derived data this way:

// ❌ BAD — clear + putAll on every composition
val merged = remember { mutableStateMapOf<Key, ViewState>() }
merged.clear()
merged.putAll(parent)
merged.putAll(overlay)

// ✅ GOOD — immutable snapshot remembered from inputs
val merged = remember(parent, overlay) {
    if (overlay.isEmpty()) parent else parent + overlay
}

If the result is read-only for the current inputs, remember(keys) { … } is enough. See compose-state-deferred-reads for cross-row measurement and measure-phase fixes.

When this rule does NOT apply

  • Inside remember { … }'s producer block. That runs once per key change, not on every recompose. A local var there is fine: val builder = remember { mutableListOf<X>().apply { var n = 0; … } }.
  • In non-@Composable lambdas passed out of a composable. onClick = { var a = 0; … } is a plain () -> Unit. Local vars there are normal Kotlin.
  • In plain (non-@Composable) helper functions. Only composable scopes are affected.

2. The @ReadOnlyComposable contract

@ReadOnlyComposable declares that a composable only reads composition state — no Text, no Box, no remember, no layout nodes, no positional slots. The runtime can then skip allocating a group for the call, which matters for fast accessor-style composables (MaterialTheme.colorScheme, LocalDensity.current, design-system token accessors).

The contract is bidirectional:

  • Add @ReadOnlyComposable when every composable call your body makes is itself @ReadOnlyComposable (or there are no composable calls at all — for example a function that only reads LocalFoo.current and returns a value).
  • Don't add it if you call any non-read-only composable. The optimization assumes you don't participate in composition; violating that produces incorrect recomposition behaviour for callers.
// ✅ GOOD — only reads composition locals, no layout, no remember
@Composable
@ReadOnlyComposable
fun appSpacing(): Dp = LocalDimensions.current.spacing

// ✅ GOOD — composable property getter; same rule
val accent: Color
    @Composable @ReadOnlyComposable
    get() = MaterialTheme.colorScheme.tertiary
// ❌ BAD — annotated read-only but lays out a Box; contract violated
@Composable
@ReadOnlyComposable
fun Header(): Int {
    Box {}                  // ← non-read-only composable call
    return 42
}

// ❌ BAD — calls a normal composable from a read-only one
@Composable
@ReadOnlyComposable
fun computed(): Int = nonReadOnlyHelper()

Heuristic for "should I add it"

If the body contains any of these, do not add @ReadOnlyComposable:

  • A layout call: Box, Column, Row, LazyColumn, Text, anything from androidx.compose.foundation.layout or androidx.compose.material*.
  • A side-effect call: LaunchedEffect, DisposableEffect, SideEffect, produceState.
  • remember { … } — positional memoization is composition state.
  • A @Composable lambda invocation (content()).
  • An invocation of a non-@ReadOnlyComposable composable function.

If the body is only reading Local*.current, calling other @ReadOnlyComposable functions, or doing pure computation, add it.

When this rule does NOT apply

  • override fun declarations. The annotation is part of the contract; if the base isn't @ReadOnlyComposable, you can't make an override one. Refactor the base, or accept the override pays the group-creation cost.
  • Abstract declarations. No body to check.

Related: side effects live in their own skill

If a composable needs LaunchedEffect, DisposableEffect, SideEffect, rememberCoroutineScope, rememberUpdatedState, snapshotFlow, snackbar/navigation handling, analytics, or Flow collection, use compose-side-effects.

Focus splits by question: navigation, focus state, FocusRequester ownership, behavior → compose-focus-navigation; when to call imperative requestFocus (effect timing, lifecycle, keys, API choice) → compose-side-effects.

This skill is about authoring Compose state correctly. rememberUpdatedState is effect capture state, not a general replacement for remember { mutableStateOf(...) }. Side effects have separate lifecycle and keying rules, and keeping them in one focused skill avoids two sources of truth.

Quick reference

SymptomDiagnosisFix
var x = … inside @Composable fun bodyNot recomposition-safe (§1)var x by remember { mutableStateOf(…) }
var x = … inside Column { … } / Row { … } content lambdaSame — content lambdas are @Composable (§1)Same fix
remember { mutableStateOf(list) } then .add(x) not recomposingMutation bypasses State setterUse mutableStateListOf, or replace the value: state = state + x
stateMap.clear(); stateMap.putAll(...) in composable bodyBack-writing composition → compositionremember(keys) { derivedSnapshot }
@Composable fun with no Text/Box/remember/effect callsCould be @ReadOnlyComposable (§2)Add @ReadOnlyComposable above @Composable
@ReadOnlyComposable function that calls Box {} / Column {} / a normal composableContract violation (§2)Remove @ReadOnlyComposable

When NOT to apply

  • Tests with composeTestRule.setContent { … } follow the same rules — they're production composables.
  • produceState has its own producer block that runs in a coroutine; you don't need LaunchedEffect inside it.
  • derivedStateOf has its own concerns around stability and equality — out of scope here; it's about preventing recomposition, not authoring state.
  • overrides of read-only-composable declarations: the annotation is fixed by the base; you can't add or remove it locally.

Red flags during review

ThoughtReality
"It's a small composable, the bare var is fine"Recomposition can fire at any time. The reset is non-deterministic by design — and a single bug report later.
"I'll add @ReadOnlyComposable because the function looks simple""Simple" isn't the criterion. "Makes only read-only calls" is.
"I always reach for LaunchedEffect because it's the one I know"Use compose-side-effects; effect API choice depends on lifecycle and keys.
"I'll just .add() to the remembered list"A mutableStateOf(List) doesn't observe internal mutation — use mutableStateListOf or replace the value.
"The override needs @ReadOnlyComposable to match what it does"If the base isn't @ReadOnlyComposable, you can't add it to an override. Refactor the base instead.

Вставить в README

Ссылка на карточку для вашего README — без картинок и счётчиков.

[DarkPlay: compose-state-authoring](https://darkplay.ru/ai/skills/chrisbanes-skills-compose-state-authoring)

Похожие скиллы и другие разделы каталога

Популярное в разделе «Скиллы»

Самые востребованные скиллов раздела по установкам и звёздам.

find-skillsvercel-labs · Помогает находить и устанавливать навыки агентов по запросам вроде «как сделать X»…
4 400 000 устан.
grill-memattpocock · Настойчивое интервью для доработки плана или дизайна.
1 600 000 устан.
hyperframesheygen-com · Обязательная точка входа для любых задач по видео, анимации и motion-графике в…
1 600 000 устан.
hyperframes-cliheygen-com · Цикл разработки HyperFrames CLI: init, add, render, preview, publish и другие команды.
1 600 000 устан.
agent-browservercel-labs · CLI для автоматизации браузера для ИИ-агентов: навигация по страницам, заполнение форм…
1 600 000 устан.
media-useheygen-com · Agent Media OS: единый навык для медиа в HyperFrames — BGM, SFX, изображения, голос, LUT.
1 500 000 устан.
hyperframes-registryheygen-com · Установка и подключение блоков реестра в композиции HyperFrames через add и catalog.
1 400 000 устан.
grill-with-docsmattpocock · Настойчивое интервью для доработки плана или дизайна, попутно создающее документацию (ADR…
1 300 000 устан.
improve-codebase-architecturemattpocock · Поиск в кодовой базе возможностей для углубления, визуальный HTML-отчёт о них и детальный…
1 300 000 устан.
tddmattpocock · Разработка через тестирование (TDD): red-green-refactor и интеграционные тесты.
1 300 000 устан.
Похожее в других разделах каталога

Соседние разделы каталога: возможно, нужный инструмент живёт там.

Flutter Revieweraffaan-m · Ревьюер кода Flutter и Dart: виджеты, управление состоянием, идиомы и архитектура.
225 587 ★
Harmonyos App Resolveraffaan-m · Эксперт по HarmonyOS на ArkTS и ArkUI: состояние V2, навигация, API и производительность.
225 587 ★
Kotlin Revieweraffaan-m · Ревьюер Kotlin и Android/KMP: корутины, Compose, архитектура и типичные ошибки.
225 587 ★
ui-ux-pro-maxnextlevelbuilder · Профессиональные знания по UI/UX-дизайну для ИИ-ассистентов программирования. Включает…
151 010 ★
Ux Designershanraisshan · Создаёт краткий и понятный UX-бриф с потоками, состояниями и аннотациями.
61 912 ★
cli-anythinghkuds · Создавайте мощные CLI-интерфейсы с сохранением состояния для любого GUI-приложения по…
5 комп.
data-engineeringanthropics · Плагин для инженерии данных: исследование хранилища, создание пайплайнов, интеграция с…
100 комп.
expoanthropics · Официальные навыки Expo для создания, развёртывания, обновления и отладки приложений…
100 комп.
mcp-tunnelsanthropics · Подключение Claude к приватному MCP-серверу через туннель Anthropic MCP. Команда…
100 комп.
netsuite-suitecloudanthropics · Навыки агента NetSuite от Oracle — руководство по созданию объектов SuiteCloud…
100 комп.

Лучшие ИИ-инструменты для программирования

ИИ для программирования

Подборка нейросетей и ИИ-ассистентов для разработки и автоматизации кода. Сервисы, которые ускоряют написание, ревью и отладку, экономят часы рутины и снижают порог входа в сложные

GenAPIGenapi
По запросу
5.0
Notepad++ AINotepad-plus-plus
По запросу
4.9
GitHub CopilotGitHub
По подписке
4.9
Creatium MaxCremax
По запросу
4.8
Emacs AIGnu
По запросу
4.8
KiteKite
По запросу
4.8
Amazon Q DeveloperAmazon
По запросу
4.8
VECTA
Есть бесплатный тариф
4.8
SoftPractice
Есть бесплатный тариф
4.8
VibeForge
Есть бесплатный тариф
4.7

Лучшие ИИ-инструменты для генерации видео

Генерация видео

Подборка нейросетей для генерации и монтажа видео. Инструменты, которые превращают текст в готовые ролики, экономят часы на съемках и постпродакшне. Подходит для SMM-специалистов,

VEED.IOVeed
По запросу
5.0
MootionMootion
По запросу
5.0
FlyviFlyvi
Есть бесплатный тариф
4.9
Google Veo 3.1Google
Платно
4.9
Sora (OpenAI)OpenAI
Платно
4.9
PikaPika
Платно
4.9
captions space
Есть бесплатный тариф
4.8
Клайп
Есть бесплатный тариф
4.8
viralpulse.appViralpulse
По запросу
4.8
Viral Maker AIViral-maker
Есть бесплатный тариф
4.8

Лучшие ИИ-инструменты для генерации текста и копирайтинга

Генерация текста и копирайтинг

Подборка нейросетей для генерации текстов и копирайтинга. Сервисы, которые помогают создавать статьи, посты и рекламные объявления с нуля или по черновику, экономя часы рутинной ра

NeuroscribeNeuroscribe
Есть бесплатный тариф
5.0
GoGPTGogpt
По запросу
5.0
ChatsonicChatsonic
По запросу
4.9
НейроТекстерNeuro-texter
По запросу
4.9
ChatGPTOpenAI
По подписке
4.9
Редактум
Есть бесплатный тариф
4.8
ChadGPTChadgpt
По запросу
4.8
Cosmo ScribeCosmoscribe
Есть бесплатный тариф
4.8
Memo HelperCopilot33
По запросу
4.8
LibraChatLibrachat
Есть бесплатный тариф
4.8