Arjan van Wieringen
04/27/2023, 5:41 PM@Composable fun Icons(
icons: List<IconState> = listOf(),
iconEventHandler: IconEventHandler? = null,
) {
icons.forEach { element ->
Icon(
iconState = element,
onClick = { iconEventHandler?.onClick(element) },
onDragStart = { offset -> iconEventHandler?.onDragStart(element, offset) },
onDrag = { offset -> iconEventHandler?.onDrag(element, offset) },
onDragEnd = { iconEventHandler?.onDragEnd(element) }
)
}
}
The icons states are the same, but the lambda's passed to the Icon composable have different hashcodes every time, and as such it rerenders. I guess it makes sense, because Kotlin can not determine structural equality of lambdas. However, this must be a common pitfall then? What could I do to improve the performance? Push the iconEventHandler
insode the Icon
composable?Ian Lake
04/27/2023, 6:40 PMrememberUpdatedState
? That's what can capture a lambda as a long-lived state object that doesn't change hashcodes: https://developer.android.com/jetpack/compose/side-effects#rememberupdatedstateArjan van Wieringen
04/27/2023, 7:04 PMIan Lake
04/27/2023, 7:09 PMArjan van Wieringen
04/27/2023, 7:27 PMFrancesc
04/28/2023, 2:09 AMList
is not stable, if you want to avoid recompositions with collections consider using immutable collectionsArjan van Wieringen
04/28/2023, 5:17 AM