Question about recomposition with `List` and `Snap...
# compose
r
Question about recomposition with
List
and
SnapshotStateList
Using
List
instead of
SnapshotStateList
with
items.forEachIndexed
causes recomposition of all the composables. For example in the below code when I click
Button A
it calls a function in viewmodel which increments the
progressState
by 10. This state change should recompose only the
A
composable but it causes recomposition of
B
as well. Any idea why ? Example in the thread 🧵
c
@Ravin Jain can you edit your post to put the code in this thread please? https://kotlinlang.slack.com/archives/CJLTWPH7S/p1616265877303000
r
Here is the example code
Copy code
@Composable
fun Screen(
    viewModel: MainViewModel = hiltViewModel()
) {
    val viewmodeState by viewModel.progressState.collectAsState()
    var rememberState by remember { mutableStateOf(1) }
    val items = remember { mutableStateListOf("1", "2", "3") }

    Column(
        verticalArrangement = Arrangement.SpaceEvenly,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Button(
            modifier = Modifier.padding(bottom = 10.dp),
            onClick = { viewModel.progress() }
        ) {
            Text(text = "Button A")
        }

        Button(
            modifier = Modifier.padding(bottom = 10.dp),
            onClick = {
                rememberState++
            }
        ) {
            Text(text = "Button B")
        }

        A(state = viewmodeState)
        B(state = rememberState, items = items)
    }
}

@Composable
fun A(state: Double) {
    Row {
        Log.d("Ravin", "A : Row")
        Text("$state", color = Color.White)
    }
}

@Composable
fun B(state: Int, items: List<String>) {
    Column {
        Row {
            Log.d("Ravin", "B : Row")
            Text("$state", color = Color.White)
        }

        Column {
            items.forEachIndexed { index, s -> }
        }
    }
}
Copy code
@HiltViewModel
class MainViewModel @Inject constructor() : ViewModel() {

    private var _progressState = MutableStateFlow(0.0)
    val progressState: StateFlow<Double>
        get() = _progressState

    fun progress() {
        viewModelScope.launch {
            for (i in 0..100 step 10) {
                delay(500)
                _progressState.value = i.toDouble()
            }
        }
    }
}
@Adam Powell any thoughts ?