Home > database >  How to pass pass values to constructor when instantiate kotlin class
How to pass pass values to constructor when instantiate kotlin class

Time:01-19

Assuming I have a class like this

class Foo(private val someVal: String) : RComponent<SomeProp, SomeState>(){
 ...
}

which I instantiate like so

child(
    Foo::class
) {
    attrs.bar = props.bar
    attrs.bar2 = props.bar2
    ...
}

How would I pass a value for someVal when instantiating the class? Note, this is KotlinJS with the React Framework.

CodePudding user response:

I'm not 100% sure, because I haven't worked with Kotlin/React in a while, but I think you can't. When using child(Foo::class), you're not responsible anymore for creating the instance, so you have to rely on the props object (SomeProps here) and set the props values. This is actually how react works: parameters for a component should be passed via props.

Also you might want to consider the new DSL for Kotlin React: https://github.com/JetBrains/kotlin-wrappers/blob/master/CHANGELOG.md#pre282

  • Related