Is it possible to store a @Composable function as ...
# compose-web
h
Is it possible to store a @Composable function as an attribute and execute it later? I want to implement a table, which holds the composable rows and display/execute the composable row function lazily when needed
Copy code
data class Cell(val color: Color?, val content: @Composable () -> Unit)
SPOILER: I get this error message:
this._$cell_0._content is not a function. (In 'this._$cell_0._content($composer_0, 0)', 'this._$cell_0._content' is undefined)
To play around: Use the defined Table in https://github.com/hfhbd/bootstrap-compose
o
This definitely should be possible. Although there are some known issues when passing composables to constructors (when targeting js). But your case looks new. Thanks for reporting. I'll check it
h
@Oleksandr Karpovich [JB] Currently,
content
is always undefined. I am adding a failing test to a
dev
branch
Thanks for the hint with the constructor! It is working using a non-constructor variable
And even
lateinit var
works 🚀
1
g
For what it's worth, I am seeing the same issue, using 0.5.0-build228.
I think I found another workaround. I noticed that Pair and Triple worked ok when used with an @Composable as one of the types. So if you define a new class with a generic type, you can then use a constructor parameter with an @Composable.
I see that https://github.com/JetBrains/compose-jb/issues/746 is closed, but I think I'm still running into this issue, using kotlin 1.5.21 and compose-web 1.0.0-alpha2. Another symptom is that a String constructor parameter is getting the value of "undefined" (I assume this is a Javascript thing?). So perhaps the "not a function" error message is a result of all constructor parameters being unitialized.
And if I use the trick of defining the Composable function type with a type parameter, the issue does not occur.