Muhammad Ahmed AbuTalib
Muhammad Ahmed AbuTalib

Reputation: 4322

Why does ImmutableList cause recomposition in Jetpack Compose?

In this article, it says that if the argument type of a composable is ImmutableList, it is considered as stable, meaning that if the list didn't change, the composable won't be recomposed.

@Immutable
data class Contact(val name: String, val age: Int)


@Composable
fun ContactRow(contacts: ImmutableList<Contact>, modifier: Modifier = Modifier) {
  var selected by remember { mutableStateOf(false) }
  Row(modifier) {
    ContactDetails(contacts)
    Checkbox(selected, onCheckedChange = {
      selected = !selected
    })
  }
}

@Composable
fun ContactDetails(contacts: ImmutableList<Contact>) {
  Text(text = contacts[0].name)
}

Here, every time I select the checkbox, the ContactDetails composable is recomposed, even though I am using ImmutableList from KotlinX collections.

My compose version is also 1.2.0

My Compiler reports also mark this as unstable

Upvotes: 7

Views: 5472

Answers (2)

Dale Evans
Dale Evans

Reputation: 11

I had this issue also until I updated my compose and Kotlin version https://developer.android.com/jetpack/compose/setup.

After which my data class with a ImmutableList in became stable. So prehaps you need to update to newer versions too?

Upvotes: 1

Denis Rudenko
Denis Rudenko

Reputation: 734

There has to be a small mistake on your side. Code presented here is valid, ContactDetails will be skipped, not recomposed. Compiler marks both ContactRow and ContactDetails as stable & skippable.

enter image description here

enter image description here

Upvotes: 1

Related Questions