Skip to content

Instantly share code, notes, and snippets.

@theapache64
Created September 24, 2021 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theapache64/662e72610fc046abe19c32706e97cd02 to your computer and use it in GitHub Desktop.
Save theapache64/662e72610fc046abe19c32706e97cd02 to your computer and use it in GitHub Desktop.
import androidx.compose.foundation.layout.Column
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.tooling.preview.Preview
import kotlinx.coroutines.delay
@Preview
@Composable
fun RecompositionTest() {
var counter by remember { mutableStateOf(0) }
val list = listOf("compose") // does not change
LaunchedEffect(Unit) {
while (true) {
delay(1000L)
counter++
}
}
Content(list, counter)
}
@Composable
private fun Content(list: List<String>, counter: Int) {
Column {
Text(text = "count $counter")
MyList(list = list) // this will always recompose because List is not @Stable
MyList(listWrapper = ListWrapper(list)) // only called once, ListWrapper is @Stable
}
}
@Composable
private fun MyList(list: List<String>) {
Column {
list.forEach {
Text(text = it)
}
}
}
@Composable
private fun MyList(listWrapper: ListWrapper) {
Column {
listWrapper.list.forEach {
Text(text = it)
}
}
}
@Stable
data class ListWrapper(val list: List<String>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment