Arpit Shukla
11/26/2021, 3:23 PM@Immutable
might fix the issue but it didn't. How can I prevent these unnecessary recompositions? Code in 🧵Arpit Shukla
11/26/2021, 3:24 PM@Immutable
data class EqualSplit(
val name: String,
val image: String,
val number: String,
val selected: Boolean
)
// In View Model
val equalSplits = mutableStateListOf<EqualSplit>()
fun onSplitSelectionChange(index: Int) {
equalSplits[index] = equalSplits[index].let { it.copy(selected = !it.selected) }
}
LazyColumn {
itemsIndexed(viewModel.equalSplits) { index, split ->
EqualSplitRowItem(
split = split,
onSelectionChange = { viewModel.onSplitSelectionChange(index) }
)
}
}
@Composable
private fun EqualSplitRowItem(
split: EqualSplit,
onSelectionChange: () -> Unit,
modifier: Modifier = Modifier
) {
Log.d("TAG", split.name) // This is printed for all items when any of the items change
Row(
modifier = modifier
.clickable(onClick = onSelectionChange)
) {
...
}
}
Jon Boekenoogen
11/26/2021, 7:39 PMArpit Shukla
11/27/2021, 4:24 AMitemsIndexed
to this:
itemsIndexed(
items = viewModel.equalSplits,
key = { _, split -> split.number }
) { index, split ->
EqualSplitRowItem(
split = split,
onSelectionChange = { viewModel.onSplitSelectionChange(index) }
)
}
It didn't work :(
@Adam Powell Does it have to do something with the onSelectionChange
lambda? Is it possible that Compose doubts the lambda might have changed and so it always recomposes everything??