Compose Mental Model & Phases Flashcards
UI › Compose Core
- What are the three Compose phases, in order?
- Composition (what to show), Layout (where to place it), Drawing (how to render it). They run in that fixed order each frame.
- Name three properties every composable function must have.
- Fast, idempotent, and side-effect free. Same inputs always produce the same UI, and it must not mutate external state during execution.
- What is recomposition and what triggers it?
- Re-running composable functions when their inputs change. Triggered by reads of changed State<T> objects (e.g. mutableStateOf) or changed parameters; Compose re-runs only affected, non-skippable composables.
- When can Compose skip recomposing a composable?
- When it returns Unit, isn't marked @NonSkippable/@NonRestartable, and all its parameters are stable types whose values are unchanged by equals().
- How does Compose identify which composable instance is which?
- By call site (source location), and for repeated calls from the same call site, by execution order. This is positional memoization.
- Why and when do you use the key() composable?
- To give explicit identity to items emitted from the same call site (e.g. in a loop). It preserves instance state and avoids needless recomposition when items are inserted, removed, or reordered.
- A state read inside Modifier.offset { } (placement lambda) re-runs which phases?
- Only layout and drawing; composition is skipped. Deferring the read to the layout phase is a key scroll-performance optimization.
- Why must composables be free of side effects, given they run in any order?
- Recomposition may skip, cancel/restart, run highly frequently, and potentially in parallel. Side effects could be dropped, duplicated, or left partially applied, causing inconsistent state. Use callbacks or effect APIs instead.
- What is a recomposition (or layout) loop and how do you avoid it?
- When state written in a later phase (e.g. onSizeChanged) is read in composition, causing an extra frame and visible jumping. Avoid it by using proper layout primitives/custom layouts with a single source of truth.