Created
September 24, 2021 08:38
-
-
Save theapache64/662e72610fc046abe19c32706e97cd02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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