我通过单击第一个可组合项调用相应的函数来更新 uiState。 第二个可组合项因此而重组,尽管它不接受任何状态参数,只接受另一个函数。
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyScetchApplicationTheme {
Screen(MyViewModel())
}
}
}
}
@Composable
fun Screen(myViewModel: MyViewModel) {
val myUiState by myViewModel.uiState.collectAsState()
Column {
MyBox(myViewModel::changeParameter) // when clicking this
MyBox(myViewModel::changeOtherParameter) // that recomposes
MyBox{ } // but that one doesn't
Text("${myUiState.otherParameter}") // and neither does that one
}
}
@Composable
private fun MyBox(function: () -> Unit) {
Box(
modifier = Modifier
.padding(20.dp)
.size(80.dp)
.background(Color.Gray)
.clickable { function() }
)
}
class MyViewModel: ViewModel() {
private var _uiState = MutableStateFlow(MyUiState())
val uiState: StateFlow<MyUiState> = _uiState.asStateFlow()
fun changeParameter() {
_uiState.update { it.copy(parameter = !uiState.value.parameter) }
}
fun changeOtherParameter() {
_uiState.update { it.copy(otherParameter = !uiState.value.otherParameter) }
}
}
data class MyUiState (
val parameter: Boolean = false,
val otherParameter: Boolean = false,
)
没有函数作为参数的相同可组合项不会重组,因此函数会以某种方式触发重组。 _ 为什么会发生这种情况,以及如何在不放弃功能的情况下避免这种重组? 由于大量不必要的重组,我的项目变得滞后。
将函数参数放入
remember
可以防止重新组合:
val onClick = remember {myViewModel.someFunction()}
感谢 @Thracian 在评论中给出的答案(也有一些有用的链接)