I'm trying to follow the examples in here <https:/...
# compose
j
I'm trying to follow the examples in here https://developer.android.com/jetpack/compose/state
Copy code
var signInState by rememberSaveable { mutableStateOf("login") }
if(signInState == "login") {
    Login(state = signInState)
}
I want to switch states by clicking the button like below
Copy code
@Composable
fun Login(state: String) {
            TextButton(
                onClick = { state = "signup" },
                colors = ButtonDefaults.textButtonColors(
                    backgroundColor = Color(0x00FFFFFF),
                    contentColor = Color.White,
                    disabledContentColor = Color(0x00FFFFFF),
                ),
            ) {
                Text("Create Account")
            }
}
and then I get an error on
Copy code
onClick = { state = "signup" },
that says
Val cannot be reassigned
i
you could do
Copy code
fun Login(state: String, onStateChange: (String) -> Unit) {}
👍 1
d
Yes, you try to reassign the argument handed over to the functions. That's not possible in Kotlin.
j
Yea i was reading the docs and it looks like function args are val's and aren't mutable
👍 1
d
Ah as @itnoles answered a little faster. use a callback to to change your state. The example in the state hoisting doc might be a good starting point https://developer.android.com/jetpack/compose/state#state-hoisting
j
oh ok I see now
Copy code
onStateChange = { signInState = it }
is the callback which I run on button click
Copy code
onClick = { onStateChange("signup") }
👍 2
i
why not
Copy code
onClick = onStateChange
?
j
I'm using the string state to switch between a login/signup/password reset view
Copy code
var signInState by rememberSaveable { mutableStateOf("login") }
if(signInState == "login") {
    Login(onStateChange = { signInState = it })
}else if(signInState == "signup"){
    Signup(onStateChange = { signInState = it })
}else if(signInState == "forgot"){
    ForgotPassword(onStateChange = { signInState = it })
}
Am I able to set the string another way? Other than
Copy code
onClick = { onStateChange("forgot") },
i
Copy code
Login(onStateChange = { signInState = "signup" })
👍 1