The SideEffect function is a scope of code triggered outside of the Compose Function. I found a way to differentiate them.
If I run it as below
@Composable
fun TrySideEffect() {
var timer by remember { mutableStateOf(0) }
Box(contentAlignment = Alignment.Center) {
Text("Time $timer")
}
Thread.sleep(1000)
timer++
}
The above code will only show 0. The timer++ has no impact, as it was changed while it is being composed, given it's part of the composable function.
However, if we use SideEffect as shown below, given it is not part of the compose function, the timer++ will trigger this, and this will make the composable function recompose again and again (Given SideEffect is being called on each Composable). This will make the Text show 0, 1, 2, 3, 4...
@Composable
fun TrySideEffect() {
var timer by remember { mutableStateOf(0) }
Box(contentAlignment = Alignment.Center) {
Text("Time $timer")
}
SideEffect {
Thread.sleep(1000)
timer++
}
}
Additional Info
To make it a little interesting, if I put on the below code, then the text will display 0, 2, 4, 6 ... (given the first ++timer will happen without composing, and the ++timer that happen in the SideEffect will trigger it)
@Composable
fun TrySideEffect() {
var timer by remember { mutableStateOf(0) }
Box(contentAlignment = Alignment.Center) {
Text("Time $timer")
}
SideEffect {
Thread.sleep(1000)
timer++
}
Thread.sleep(1000)
timer++
}
Another interesting note, comparing SideEffect with LaunchEffect
If we use LaunchEffect, the number will only increment once, i.e. from 0 to 1. This is because unlike SideEffect, LaunchEffect only triggered on the first recomposition, and not change on the subsequent recomposition (unless we change the key1 value, so it will be triggered upon change of key1 value.).
@Composable
fun TrySideEffect() {
var timer by remember { mutableStateOf(0) }
Box(contentAlignment = Alignment.Center) {
Text("Time $timer")
}
LaunchEffect(key1 = Unit) {
delay(1000) // or Thread.sleep(1000)
timer++
}
}