Skip to content

Instantly share code, notes, and snippets.

@shahzadansari
Created November 22, 2023 19:09
Show Gist options
  • Save shahzadansari/47962aadd7afce3d25d292070cc7e5bc to your computer and use it in GitHub Desktop.
Save shahzadansari/47962aadd7afce3d25d292070cc7e5bc to your computer and use it in GitHub Desktop.
SideEffect Demo (runs every time composable gets recomposed)
Box(modifier = Modifier.fillMaxSize()) {
var count by remember { mutableIntStateOf(0) }
StateDependentComposable(count)
Button(
onClick = { count++ },
content = { Text("Increment") },
modifier = Modifier.align(Alignment.Center)
)
}
@Composable
fun StateDependentComposable(count: Int) {
SideEffect {
Log.d("MyTag:", "Count: $count")
}
}
@shahzadansari
Copy link
Author

When count changes, composable gets recomposed, resulting it executing the SideEffect block

side-effect.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment