Is it expected that recomposition will not happen if the `data class` which represents my UI state c...
z
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
I'm just a beginner, but I think Compose only detects changes to the
State
class.
z
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
Yea, Compose has no way to know if arbitrary properties in classes change, unless they’re backed by snapshot state objects.
z
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
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
nice, great explanation! thank you both