Facing recomposition issue, more in the :thread:
# compose
r
Facing recomposition issue, more in the 🧵
Copy code
@Stable
data class DummyData(val progress: Float = 0F, val flag: Boolean = false)

class DummyViewModel : ViewModel() {
    val state = MutableStateFlow(DummyData())

    fun toggleFlag() {
        state.value = state.value.copy(flag = !state.value.flag)
    }

    fun updateProgress(progress: Float) {
        state.value = state.value.copy(progress = progress)
    }
}

//In Activity
//On updating progress or flag, Scaffold, onProgressUpdate, toggleFlag recomposing if any state property changes
setContent {
    AppTheme {
        val state: DummyData by viewModel.state.collectAsState()
        Scaffold(
            state,
            onProgressUpdate = { viewModel.updateProgress(it) },
            toggleFlag = { viewModel.toggleFlag() }
        )
    }
}

//Use method reference to fix recomposition.
//Scaffold with either onProgressUpdate or toggleFlag will recompose if their value changes
setContent {
    AppTheme {
        val state: DummyData by viewModel.state.collectAsState()
        Scaffold(
            state,
            onProgressUpdate = viewModel::updateProgress,
            toggleFlag = viewModel::toggleFlag
        )
    }
}
I’ve found this article which does the same thing, Its hard to identify these kind of issues while coding, is there any lint checks available to avoid this issue? Whenever parent recompose lambdas hascode is changing which results in recomposition of child compose functions that consume these lambdas