Skip to content

Lesson 1 of 9 · 27m

The Compose Mental Model: State In, Pixels Out

Understand composition, recomposition, state reads, and why Compose rewards unidirectional data flow instead of view mutation.

The first week with Jetpack Compose feels magical; the second week is usually when someone mutates a list in place and asks why the screen did not update. Compose is not XML with nicer syntax. It is a runtime that executes Kotlin functions, remembers values, observes state reads, and reruns selected functions when that state changes. If you keep the old Android View habit of reaching into a widget and changing it, you will build a confused hybrid with the worst bugs of both worlds.

Composition is a function execution, not a widget tree you own

A composable describes UI for some state:

@Composable
fun OrderSummary(order: Order, onCancel: () -> Unit) {
    Column(modifier = Modifier.padding(16.dp)) {
        Text(order.title, style = MaterialTheme.typography.titleLarge)
        Text("Total: ${order.total}")
        Button(onClick = onCancel) { Text("Cancel order") }
    }
}

When order changes, Compose may recompose OrderSummary. You do not tell the Text to update; you emit a new description. This is why immutable inputs matter. A mutable object whose fields changed in place may compare equal to itself, leaving the runtime with no reason to invalidate the UI.

State is read-position sensitive

Compose tracks where state is read. This example is subtly different from reading count at the top level:

@Composable
fun Counter() {
    var count by remember { mutableIntStateOf(0) }
    Button(onClick = { count++ }) {
        Text("Clicked $count times")
    }
}

The state read inside Text lets Compose limit recomposition to the parts observing that state. Moving the read higher or lower changes the invalidation scope. You do not need to micro-optimize every read on day one, but you do need to stop thinking of recomposition as a full-screen repaint.

remember is not a database

remember stores a value across recomposition, not across configuration changes or process death:

var query by remember { mutableStateOf("") }
TextField(value = query, onValueChange = { query = it })

Rotate the phone and composition state may be recreated. Use rememberSaveable for small UI-restorable values, and a ViewModel for state owned by a screen or feature. Anything the business cares about belongs below the UI layer, not inside a composable closure.

Stability and keys are performance contracts

Compose can skip recomposition when inputs are stable and unchanged. Data classes with immutable properties are good citizens. Collections wrapped in mutable types, lambdas capturing unstable objects, and huge composables that read unrelated state reduce skipping. In lazy lists, identity matters:

items(orders, key = { it.id }) { order ->
    OrderRow(order)
}

Without stable keys, item state can migrate to the wrong row when the list reorders or an element is inserted.

Field notes

  • Recomposition is normal and frequent; correctness must not depend on a composable executing exactly once.
  • Never launch network work directly in composition because the function may run again or be discarded.
  • Do not use remember as a cache for data that must survive process death.
  • Mutable lists inside immutable-looking state objects produce heisenbugs: mutate and replace, or use immutable copies.
  • Measure before optimizing; most Compose performance problems are architecture problems wearing profiler clothing.

Was this lesson helpful?

Sign in to rate