z

    zsperske

    1 year ago
    Is it expected that recomposition will not happen if the
    data class
    which represents my UI state contains a
    var
    and that var changes?
    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)
    updates happen via this method
    fun updateEventDestination(destination: String) {
        val currentState = mutableStateFlow.value
        mutableStateFlow.value = currentState.copy(eventDestination = destination)
    }
    c

    CLOVIS

    1 year ago
    I'm just a beginner, but I think Compose only detects changes to the
    State
    class.
    z

    zsperske

    1 year ago
    You can see that I take the current state, and then copy it with the updated state in order to emit a new value
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    Yea, Compose has no way to know if arbitrary properties in classes change, unless they’re backed by snapshot state objects.
    z

    zsperske

    1 year ago
    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

    1 year ago
    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
    z

    zsperske

    1 year ago
    nice, great explanation! thank you both