The current verbosity in creating functional compo...
# react
a
The current verbosity in creating functional components in kotlin-react is uncanny. I would have to create the function
Copy code
external interface DumbProps {
   var name: String
}

val DumbComp = functionalComponent<DumbProps> {
    div { +"Hi, I am ${it.name}" }
}
Then, I would have to create an extension for RBuilder like so
Copy code
fun RBuilder.DumbComp(props: DumbProps,handler:RHandler<DumbProps>) = child(DumbComp,props,handler)
This makes the usage of functional components and hooks so verbose. Advice: RBuilder should add a member function to ease use of functional components and hooks like so
Copy code
class RBuilder{
  . . .
  operator fun <P : RProps> FunctionalComponent<P>.invoke(
    props: P = jsObject(),
    handler: RBuilder.(P) -> Unit
  ) = child(this, props, handler)
  . . .
}
So that now, I can just go and call
Copy code
DumbComp {
   attrs.name = "Andy" 
}
👍 4
f
@andylamax, In cases where I only have one property, I'm too lazy to create an interface. Do you know if I can reuse
RProps
?
Like This:
Copy code
val DumbComp = functionalComponent<RProps> { props ->
    div { +"Hi, I am $props["name"]" }
}
a
I Usually have a hack for simple components. I usually have more of a utility Extension function called Component, it goes like so
Copy code
fun <P : RProps> RBuilder.Component(props: P = jsObject(), builder: RBuilder.() -> Unit) = child({
    buildElements {
        builder()
    }
}.unsafeCast<FunctionalComponent<P>>(), props)
If I have a simple Component like the one you said, I just do,
Copy code
fun RBuilder.DumbComp(name:String) = Component<RProps>{
  div { +"Hi, I am ${name}}"}
}
So now you can just call your component
Copy code
DumbComp("Andy") or DumbComp(name = "Andy")