Alexander Weickmann
07/10/2020, 8:49 AMconst ProductHomeExample = () => (
<ProductHome siteTitle="test" />
);
export default () => (
<SomeComponent
renderProductHome={ProductHomeExample}
/>
);
I am trying like this:
val ProductHomeExample = {
ProductHome {
attrs.siteTitle = "Test"
}
}
SomeComponent {
attrs.renderProductHome = ProductHomeExample
}
but this leads to error:
Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component
Edit: From looking at the type signature in the js file, I can see renderProductHome is of type React.ComponentType<{}>. So the question simplifies, how to create such a React.ComponentType<{}> instance?Foso
07/10/2020, 10:16 AMAkif Abasov [JB]
07/10/2020, 10:42 AMrenderProductHome
should have type RBuilder.() -> Unit
to be called in correct way in render functions. So you need to explicitly specify ProductHomeExample
type to have RBuilder
context. Another point is - if ProductHome
is class component the pattern is to create wrapper to call it
fun RBuilder.productHome(siteTitle: String) = child(ProductHome::class) {
attrs {
this.siteTitle = siteTitle
}
}
And then you will be able to have
val ProductHomeExample: RBuilder.() -> Unit = {
productHome("Test")
}
Akif Abasov [JB]
07/10/2020, 10:45 AMSomeComponent
should have the same:
fun RBuilder.someComponent(
renderProductHome: RBuilder.() -> Unit
) = child(SomeComponent::class) {
attrs {
this.renderProductHome = renderProductHome
}
}
Akif Abasov [JB]
07/10/2020, 10:49 AMfun RBuilder.render() {
someComponent(ProductHomeExample)
}
Akif Abasov [JB]
07/10/2020, 10:53 AMchild(SomeComponent)
instead of child(SomeComponent::class)
. But you should remember that in Kotlin/JS the way to create functional component is
val SomeComponent = functionalComponent<SompeComponentProps> {
//...
}
instead of just creating function (like in JS)Alexander Weickmann
07/10/2020, 12:28 PMvar renderProductHome: () -> ReactElement
and then
attrs.renderProductHome = {
ProductHome {
attrs.siteTitle = "test"
}
}