Ravin Jain
08/19/2021, 3:05 AMList
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 🧵Colton Idle
08/19/2021, 3:12 AMRavin Jain
08/19/2021, 3:16 AM@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 -> }
}
}
}
@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()
}
}
}
}
Ravin Jain
08/19/2021, 5:16 PM