-
Survive Recomposition Use
rememberto 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
LocalCompositionProviderto implicitly pass values like spacing or themes down the composable hierarchy without prop drilling. -
Immutable Data Avoid passing mutable lists to composables. Use
ImmutableListor wrap data in a stabledata classannotated with@Immutable. -
Use @Stable or @Immutable Annotate data classes with
@Stableor@Immutableto optimize Jetpack Compose performance. These annotations signal that the object’s properties rarely change, reducing unnecessary recompositions.
Thank You!