theapache64
02/21/2022, 7:05 PMkey
actually doing to slot table? 🤔 🧵theapache64
02/21/2022, 7:05 PM@Composable
fun Test() {
val list = remember { mutableStateListOf("Audi", "BMW") }
Box {
list.firstOrNull()?.let { item ->
key(item) {
val car = remember { Car(item) }
println(car.name)
LaunchedEffect(Unit) {
list.remove(item)
}
}
}
}
}
class Car(val name: String)
The above snippet would print -> Audi, BMW
but If we remove the key(item)
, It’ll only create one object and will print Audi, Audi
I am curious what’s actually happening inside the slot table when we wrap our composable with key
Leland Richardson [G]
02/21/2022, 7:33 PMLeland Richardson [G]
02/21/2022, 7:34 PMtheapache64
02/21/2022, 8:22 PMDid you actually attempt to write code like this for a real use case?Nope 😄 I was just experimenting stuffs
Key will not reuse the slots/state unless it matches the key, so without it, your launched effect only runs onceGot it. but I was looking for a detailed explanation. Maybe am not asking it the right way. I’ll come back with a rephrased version 🏃
jim
02/22/2022, 5:15 PMkey
creates a new movable group, and gives it an identity (the value passed into the first parameter of key
. This identity is stored in the slot table, and is checked the next time the composable is re-executed to determine if the group has been added/deleted/reordered.
Does that answer your question?theapache64
02/22/2022, 5:18 PM