Compose Side Effects Interview Questions

UI › Compose Core

What is a side effect in Compose, and why do you need effect handlers?

What a strong answer covers: A side effect is any change to state outside the composable's scope, like a network call, logging an analytics event, or showing a snackbar. Since composables can re-run any number of times, effects in the body would repeat. Effect handlers (LaunchedEffect, DisposableEffect, SideEffect, rememberCoroutineScope) run effects in a controlled way tied to the composition lifecycle.

How does LaunchedEffect work, and what do its keys do?

What a strong answer covers: LaunchedEffect(key) launches a coroutine when it enters the composition, cancels it when it leaves, and cancels and relaunches whenever a key changes. LaunchedEffect(Unit) runs once per entering the composition. Choose keys carefully: a missing key means using stale values; a key that changes on every recomposition restarts the effect constantly.

When would you use DisposableEffect instead of LaunchedEffect?

What a strong answer covers: When you register something that needs cleanup and isn't a coroutine: adding a lifecycle observer, registering a BroadcastReceiver or sensor listener, or a callback. DisposableEffect(key) { register(); onDispose { unregister() } } guarantees cleanup when the composable leaves or the key changes.

What's the difference between rememberCoroutineScope and LaunchedEffect, and what are rememberUpdatedState and SideEffect for?

What a strong answer covers: rememberCoroutineScope gives a scope tied to the composition for launching from event handlers like a button click (scroll to top, show a snackbar), whereas LaunchedEffect starts from composition itself. rememberUpdatedState lets a long-running effect use the latest value of a lambda or parameter without restarting. SideEffect runs after every successful recomposition, for publishing Compose state to non-Compose code.

How do you trigger navigation or a one-off event from a ViewModel in Compose without it firing twice or getting lost?

What a strong answer covers: Approaches: keep the event as state in UiState (navigateTo: Route?) and have a LaunchedEffect(state.navigateTo) navigate and then call viewModel.onNavigated() to clear it; or collect a Channel-backed flow in LaunchedEffect with Dispatchers.Main.immediate and repeatOnLifecycle. Firing twice happens when the state isn't cleared; losing it happens when emitted while nothing collects. Calling navigate directly in the composable body fires on every recomposition.

Back to Compose Side Effects