为什么可组合的重组看起来是无状态的(唯一传递的参数是函数,而不是状态)

问题描述 投票:0回答:1

我通过单击第一个可组合项调用相应的函数来更新 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,
)

没有函数作为参数的相同可组合项不会重组,因此函数会以某种方式触发重组。 _ 为什么会发生这种情况,以及如何在不放弃功能的情况下避免这种重组? 由于大量不必要的重组,我的项目变得滞后。

kotlin android-jetpack-compose android-viewmodel kotlin-stateflow compose-recomposition
1个回答
0
投票

将函数参数放入

remember
可以防止重新组合:

    val onClick = remember {myViewModel.someFunction()}

感谢 @Thracian 在评论中给出的答案(也有一些有用的链接)

© www.soinside.com 2019 - 2024. All rights reserved.