Nikky
03/18/2021, 7:18 PMState<List<MyData>>
how would i use a layout compontn like Rowm Column or smth else to render each element in the list ?
essentially, can i do a renderEach ?Zach Klippenstein (he/him) [MOD]
03/18/2021, 7:23 PMLazyColumn
or LazyRow
theapache64
03/18/2021, 7:24 PMLazyColumn
as @Zach Klippenstein (he/him) [MOD] mentioned
LazyColumn {
items(myList.value) { item ->
Text("Item : $item")
}
}
Nikky
03/18/2021, 7:27 PMZach Klippenstein (he/him) [MOD]
03/18/2021, 7:27 PMList
is either immutable and you push a new value into the State
, or if your list is a mutableStateListOf
, then yesmutableListOf
, and you mutate the list, it won’t trigger recompositionsNikky
03/18/2021, 7:28 PMtheapache64
03/18/2021, 7:32 PMfun main() {
val myList = mutableStateListOf(
MyData("A"),
MyData("B"),
MyData("C"),
)
Window {
Column {
LazyColumn {
items(myList.size) { itemIndex ->
val item = myList[itemIndex]
Text("Item : ${item.name}")
}
}
Button(
onClick = {
myList.add(MyData("NEW: ${Date()}"))
}
) {
Text(text = "Add New Item")
}
}
}
}
Nikky
03/18/2021, 8:15 PMmyList.getOrNull(ItemIndex) ?: return@items
Adam Powell
03/18/2021, 8:25 PMNikky
03/18/2021, 8:25 PMval textState = remember(index) { mutableStateOf(TextFieldValue(item.foo)) }
i hope this makes sense.. i am new to this and jsut want to see what i can doState<MyData>
as elements in the list
and do not use remember {}
at all