Compose Side-Effects: Escaping the Pure Function

Compose functions should be pure, but apps must interact with the world. Side-effect APIs are controlled escape hatches to run non-UI code, like network calls or showing Toasts, from within a composable's lifecycle. The footgun is calling this logic directly.
Why it exists
Compose's declarative model relies on pure functions for predictable UI rendering. A pure function always returns the same output for the same input and has no other observable effects. However, apps must perform actions that affect things outside the UI, like fetching data or logging analytics. These 'side-effects' would break purity and cause chaos if run directly during composition, as composables can be re-run frequently and unpredictably.
The mental model
A composable is a pure blueprint for UI. Side-effect handlers are the designated, controlled 'construction zones' where you can safely perform real-world work. They tie non-UI logic to a composable's lifecycle, ensuring effects run at the right time (e.g., when the composable first appears) and are cleaned up properly (e.g., cancelling a network call when it leaves the screen). This isolates impurity from the rendering logic.
How it works
Compose provides a set of APIs to manage effects. The most common is LaunchedEffect. You give it one or more 'keys' (often state variables). When the composable enters the composition, it launches a coroutine to execute your effect block. If a key changes, the old coroutine is cancelled and a new one is launched with the new value. If the composable leaves the composition, the coroutine is automatically cancelled. Other handlers like DisposableEffect are for effects that need explicit cleanup logic, and SideEffect is for code that must run after every successful recomposition.
When to use it
Use side-effect handlers whenever a composable needs to trigger a one-off or state-driven action that is not directly creating UI. This includes launching coroutines for data fetching, communicating with other parts of the Android system (like showing a Toast or Snackbar), or subscribing to a data stream that requires manual cancellation.
When not to use it
Do not use side-effect handlers for logic that can be derived directly from state. For example, to show or hide a UI element based on a boolean, use a simple if statement in your composable body. If a state transformation is computationally expensive, use derivedStateOf to memoize the result. Effects are for triggering actions, not for calculating the UI tree itself.
One canonical example
Showing a Snackbar when an error occurs. A ViewModel exposes an error message state. The UI uses LaunchedEffect(key1 = errorMessage) where errorMessage is the state variable. When errorMessage changes from null to a string, the effect launches, calls snackbarHostState.showSnackbar(), and then calls a ViewModel function to clear the error. This ensures the snackbar appears exactly once per error, tied to the lifecycle of the composable.
Interview question
Which scenario most appropriately utilizes a Compose side-effect API?
- a.Triggering a network request to fetch data when a screen first appears.Correct
- b.Determining if a button should be enabled based on form input validity.
- c.Filtering a large dataset to display only relevant items in a list.
- d.Changing the background color of a composable based on a theme setting.
Why? this is the answer
Fetching data is a non-UI action that needs to be tied to the composable's lifecycle, preventing unpredictable execution during recomposition, which is the purpose of side-effect APIs. The other options describe UI calculations or derived state that should be handled directly within the composable's body or with derivedStateOf, not with side-effects.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
- #jetpack compose
- #android
- #kotlin
- #side-effects
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on jetpack compose — each one lists the topics its interview covers.
See open roles