Member-only story
Jetpack Compose Side-Effects
Imagine you’ve created an amazing Jetpack Compose app, but suddenly some strange behavior occurs! Have you ever wondered why? All this is possible due to side-effects!
In this article we will understand the concept of “side-effects” in simple and easy words: what are side-effects, why, when, and how should they be used, and what are their types while using Jetpack Compose? Every point will be clear with the example, so let’s start!
What is a side-effect?
According to official documentation, In Compose, a side-effect refers to any change in the app’s state that occurs outside the scope of a composable function.
The Side-effect in Jetpack Compose occurs when a composable function interacts with or performs any function other than its core UI rendering functionality. These are actions that are triggered when the composable is recomposed or with specific state changes, like this:
- updating the database,
- show toast or snack bar,
- log in to the console
- Making API calls to fetch data.
- Show alert or message.
Because Compose works on a reactive and declarative UI model, it is important to handle side-effects correctly so that the app’s behavior is predictable.