Having this JavaScript code, how do I need to writ...
# javascript
a
Having this JavaScript code, how do I need to write this in KotlinJS?
Copy code
const ProductHomeExample = () => (
  <ProductHome siteTitle="test" />
);
export default () => (
    <SomeComponent
      renderProductHome={ProductHomeExample}
    />
);
I am trying like this:
Copy code
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?
a
Usual in kotlin/js props like
renderProductHome
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
Copy code
fun RBuilder.productHome(siteTitle: String) = child(ProductHome::class) {
  attrs {
    this.siteTitle = siteTitle
  }
}
And then you will be able to have
Copy code
val ProductHomeExample: RBuilder.() -> Unit = {
  productHome("Test")
}
And
SomeComponent
should have the same:
Copy code
fun RBuilder.someComponent(
  renderProductHome: RBuilder.() -> Unit
) = child(SomeComponent::class) {
  attrs {
    this.renderProductHome = renderProductHome
  }
}
so after all you will be able to call it like this
Copy code
fun RBuilder.render() {
  someComponent(ProductHomeExample)
}
If ProductHome and SomeComponent are functional components code will be little different. It should be called
child(SomeComponent)
instead of
child(SomeComponent::class)
. But you should remember that in Kotlin/JS the way to create functional component is
Copy code
val SomeComponent = functionalComponent<SompeComponentProps> {
  //...
}
instead of just creating function (like in JS)
a
Thank you for help 🙂 What worked now was this:
Copy code
var renderProductHome: () -> ReactElement
and then
Copy code
attrs.renderProductHome = {
    ProductHome {
        attrs.siteTitle = "test"
    }
}