I would like to build two components in Kotlin/Rea...
# javascript
d
I would like to build two components in Kotlin/React:
MyElementsList
and
MyElement
Copy code
MyElementsList(content = {
    MyElement(label = "a", value = "1")
    MyElement(label = "b", value = "2")
    MYElement(label = "c", value = "3")
})
what should be the definition of MyElementsList’s
content
parameter to be able to pass a block of components ? currently this is what I wrote, but it doesn’t seem to work:
Copy code
fun RBuilder.ElementsList(content: (ReactElement) -> Unit) = child(ElementsList) {
    attrs {
        this.content = content
    }
}

external interface ElementsListProps : RProps {
    var content : (ReactElement) -> Unit
}

val ElementsList = functionalComponent<ElementsListProps> { props ->
    div {
        props.content
    }
}
it compiles, but it just shows
<div></div>
with no content the
MyElement
components are shown correctly if they are not enclosed in the
MyElementsList
component, so it looks like the issue is in the
MyElementsList
component
b
Copy code
fun RBuilder.ElementsList(content: RBuilder.() -> Unit) = child(ElementsList) {
    attrs {
        this.content = content
    }
}
external interface ElementsListProps : RProps {
    var content : RBuilder.() -> Unit
}
and when calling the prop content. you call it like this:
Copy code
props.content.invoke(this)
d
I get this issue:
@Bahaa Kallas
b
ah.. you have external keyword. Its not needed when define props interfaces. It will work without it
d
I was using it, because that’s how it is in the tutorial:
b
is
ElementsList
a component defined in your kotlin code or imported through
npm
(its javascript component) ? for any component you create within your kotlin code you will never use the
external
keyword
external
indicate that the following (class-function-interface) is defined in javascript and not in your kotlin code
d
ok, thanks, I get it now! yes,
ElementsList
is defined in my Kotlin code
fantastic, I just tried your code suggestion, with
RBuilder.() -> Unit
and
invoke(this)
and it works!
@Bahaa Kallas many thanks! this was a great step for me!
👍 1
I spent 5 hours yesterday trying all different combinations and I couldn’t make it work 😓
😢 1