https://kotlinlang.org logo
#javascript
Title
# javascript
d

Don Kittle

03/10/2023, 8:27 PM
Question about passing props when using Kotlin/JS & React. When you create an element using
Copy code
element = About.create()
How does one go about passing props to the element on creation?
t

turansky

03/10/2023, 9:21 PM
Copy code
element = About.create {
    // your props
}
d

Don Kittle

03/10/2023, 9:45 PM
Thanks!
Hmm, I must be missing something obvious. The prop is undefined in the child component. This is my parent component. The log statement prints out the webUser's properties.
Copy code
Route {
   path = "/campaigns"
   element = Campaigns.create {
      webUser
   }
   console.log("web user ->>> $webUser")
}
My child is set up like this and webUser is undefined....
Copy code
external interface CampaignWebUserProps: Props {
    var webUser: WebUser
}

val Campaigns = FC<CampaignWebUserProps> {props ->

    val webUser by useState(props.webUser)
...
}
Am I missing something obvious?
t

turansky

03/10/2023, 11:08 PM
Copy code
element = Campaigns.create {
    this.webUser = webUser
}
If user data is common - it’s preferrable to use React
Context
instead
d

Don Kittle

03/10/2023, 11:11 PM
AHHHH, thanks so much! That works 🙂
Ok, I'll read up on
Context
right now
I also just realize I can handle "injecting" bearer and dealing with refresh automagically with Ktor client so I won't need to do this (above) much longer. But I'm sure I'll want to pass props from parent to child sometime.
t

turansky

03/10/2023, 11:15 PM
Components, which I use for routes have no props. With
Context
you avoid redundant rendering for components, which don’t use data directly (for proxies)
🙏 1
👍 1
In next version it will be possible to use
ComponentType
instead
4 Views