'How do I pass the onClick parameter in this one?

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.



Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1