I want to pass state but it throws error "val cannot be reassigned".
@Composable
fun MyUi() {
var showContent by remember { mutableStateOf(false) }
if (showContent) {
MyButton(showContent)
}
}
@Composable
fun MyButton(showContent: Boolean) {
// very long content
Button(onClick = {
showContent = true
}) {
Text(text = "Very long content")
}
}
>Solution :
You should make your composables statelesss and pass events up, change it like this, where the top composable is responsible for mutating the flag
@Composable
fun MyUi() {
var showContent by remember { mutableStateOf(false) }
if (showContent) {
MyButton(
onClick = {
showContent = true
}
)
}
}
@Composable
fun MyButton(onClick: () -> Unit) {
// very long content
Button(onClick = onClick) {
Text(text = "Very long content")
}
}