This is my OTP screen function to receive and read OTP.
The function has the 2 parameters as shown,
fun OTPScreen(
navController : NavController,
onClick: (mobileNum: String,otp: String) -> Unit,
)
This is my navhost
function that navigates to the OTP screen.
Can someone tell me how do I pass the onClick parameter in this case?
composable(
route = Screen.Otpscreen.route,
) {
OTPScreen(navController = navController, onClick = (" "))
}
I'm new to jetpack and functions are always confusing.
Let me know if I need to elaborate further.
CodePudding user response:
I'm not familiar with compose but assuming it works like any other kotlin you could write it like this
OTPScreen(navController = navController, onClick = {mobileNum, otp ->
//your code here
})
as noted by Karsten Gabriel, this can also be written shorter as
OTPScreen(navController) { mobileNum, otp ->
//your code here
}
Or, if you want to use an existing function like
fun myFunction(mobileNum: String, otp: String) {
//your code here
}
for example you can use that like
OTPScreen(navController = navController, onClick = ::myFunction)
CodePudding user response:
Assuming you know nothing about state-hoisting, just copy and paste this logic, do not question it
fun OTPScreen(
navController : NavController,
onClick: (mobileNum: String,otp: String) -> Unit,
){
//Here you must have access to the num and otp entered by the user, just call the onClick
onClick(usrNum, usrOtp)
}
Now, at the calling site,
composable(
route = Screen.Otpscreen.route,
) {
OTPScreen(
navController = navController,
onClick = { num, otp -> //These are the values received from the user's screen
verifyOtpForNumber(num, otp) //whatever
}
)
}
If you don't understand this, just paste it right out,
Read here to learn