In Kotlin, function is a first-class citizen. We can store a function in a variable as below
val functionVariable: () -> Unit = ::myFunction
fun myFunction() { }
However, for @Composable
function, how can I do so?
If I did the below, it will cry foul i.e. org.jetbrains.kotlin.diagnostics.SimpleDiagnostic@e93b05f8 (error: could not render message)
val functionVariable: () -> Unit = ::myFunction
@Composable
fun myFunction() { }
Is there a way to store composable function as a variable?
Composable function reference is not yet supported (and that's what the error message actually is). Besides, @Composable
annotation is part of the function signature because it adds some parameters to the function. So you need to use val functionVariable: @Composable () -> Unit = { myFunction() }
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With