Colton Idle
09/25/2020, 2:11 AM@Composable
private fun MyActivityScreen(items: List<String>) {
val myThings = remember { mutableStateOf(items) }
Column() {
myThings.value.forEach {
Text(text = it)
}
}
}allan.conda
09/25/2020, 2:21 AMmyThings is not necessary I think. Use items directly.Kane Shih
09/25/2020, 2:25 AMColton Idle
09/25/2020, 3:08 AMColton Idle
09/25/2020, 4:08 AMKane Shih
09/25/2020, 4:15 AMmyThings.setValue
2 Kotlin Flow or RxJava -> to StateColton Idle
09/25/2020, 4:16 AMKane Shih
09/25/2020, 4:16 AMKane Shih
09/25/2020, 4:17 AMColton Idle
09/25/2020, 4:49 AMval myThings = remember { mutableStateOf(items) } is used for? @Leland Richardson [G] am I doing something wrong here?flosch
09/25/2020, 6:24 AMColton Idle
09/25/2020, 6:26 PMval listOfItems = mutableListOf<String>()
so if I just change that to mutableStateOf I should be good? I will try that now and let you know what happens. Thank you! I was not aware that I couldn't just use a regular list and pass it into a composable and then that composable converts it to an observable (aka. thats what I thought I was doing with my val myThings = remember { mutableStateOf(items) } statementflosch
09/25/2020, 6:28 PMmyThings then it will be recomposed, but you said you changed items outside of your composable; that will not workColton Idle
09/25/2020, 6:30 PMColton Idle
09/25/2020, 6:38 PM@Composable
private fun MyActivityScreen(items: MutableState<MutableList<String>>) {
Column() {
items.value.forEach {
Text(text = it)
}
}
}
And my activity defines a field
val listOfItems = mutableStateOf(mutableListOf<String>())
and I add to the listOfItems via
listOfItems.value.add("adsf")flosch
09/25/2020, 7:25 PMMutableState and not of the MutableList
val listOfItems = mutableStateOf(listOf<String>())
@Composable
private fun MyActivityScreen(items: State<List<String>>) {
Column() {
items.value.forEach {
Text(text = it)
}
}
}
listOfItems.value = listOf("a")
listOfItems.value = listOf("a", "b" )
You could also use mutableStateListOf (LINK)Colton Idle
09/26/2020, 1:53 AMflosch
09/26/2020, 11:23 AMdata class State(val items: List<String>, ...)
val state = ...
state.copy(items = ...)
Immutable objects are the way to go with data driven architecturesColton Idle
09/27/2020, 6:55 PM