https://kotlinlang.org logo
#compose
Title
# compose
z

zsperske

08/02/2021, 7:24 PM
Is it expected that recomposition will not happen if the
data class
which represents my UI state contains a
var
and that var changes?
Copy code
data class EventListState(val events: List<EventUiModel> = emptyList(),
                          var eventDestination: String = "", //Changes to this var do not cause recomposition
                          val pastEventsSelected: Boolean = false,
                          val isLoading: Boolean = true)
👌 1
updates happen via this method
Copy code
fun updateEventDestination(destination: String) {
    val currentState = mutableStateFlow.value
    mutableStateFlow.value = currentState.copy(eventDestination = destination)
}
c

CLOVIS

08/02/2021, 7:28 PM
I'm just a beginner, but I think Compose only detects changes to the
State
class.
z

zsperske

08/02/2021, 7:30 PM
You can see that I take the current state, and then copy it with the updated state in order to emit a new value
z

Zach Klippenstein (he/him) [MOD]

08/02/2021, 7:32 PM
Yea, Compose has no way to know if arbitrary properties in classes change, unless they’re backed by snapshot state objects.
z

zsperske

08/02/2021, 7:33 PM
Can you expand on that a little bit? If I update the data class and change that
var
to a
val
, then things work as expected
c

CLOVIS

08/02/2021, 7:35 PM
If you use a
var
, you are editing the object directly. Compose cannot know that. If you use a
val
, you can't edit the object, so you edit its reference: if that reference is handled by an object Compose understands (State, StateFlow...) then Compose knows about it
💯 2
☝🏻 1
z

zsperske

08/02/2021, 7:43 PM
nice, great explanation! thank you both