Mukul J. · 24 September 2024
Survive Recomposition
Use remember
to retain state across recompositions. For example, store a text field’s value to prevent resetting during UI updates.
Use Side Effects LaunchedEffect which is used for safely launch coroutines (e.g., fetching data) tied to a composable’s lifecycle. DisposableEffect which is used for clean up resources (e.g., listeners) when a composable leaves the screen.
Optimize State
Convert multiple states into one with derivedStateOf (e.g., deriving a Boolean from a list’s size). Use produceState to wrap non-Compose states (e.g., Flow
) into Compose-compatible states.
List Performance Always provide a unique key in LazyColumn items and ImmutableList to help Compose identify changes efficiently and skip unnecessary recompositions.
State Hoisting Place modifiers and state parameters at the top of composable functions for flexibility. For example, pass padding or click handlers as parameters instead of hardcoding them.
CompositionLocal
Use LocalCompositionProvider
to implicitly pass values like spacing or themes down the composable hierarchy without prop drilling.
Immutable Data
Avoid passing mutable lists to composables. Use ImmutableList
or wrap data in a stable data class
annotated with @Immutable
.
Use @Stable or @Immutable
Annotate data classes with @Stable
or @Immutable
to optimize Jetpack Compose performance. These annotations signal that the object’s properties rarely change, reducing unnecessary recompositions.
Thank You!