Hello! Is there a way to use PropTypes in Kotlin JS React? Or some other way / workaround to enforce some properties to be passed to component?
I could do a function decorating component, but I have no idea how to create lambda which would use both ChildrenBuilder AND my Properties:
So we got example component:
Copy code
external interface ExampleProps : Props {
var optionalProp: String?
var mandatoryProp: String
}
val Example = FC<ExampleProps> { props ->
}
Decorator:
Copy code
fun ChildrenBuilder.ExampleDecorator(mandatory: String, prop: ChildrenBuilder.(props: ExampleProps) -> Unit) {
Example {
prop(this)
mandatoryProp = mandatory
}
}
and usage:
Copy code
val usage = FC<Props> {
Example {
// Here this is ChildrenBuilder & ExampleProps & Props
}
ExampleDecorator("abc") {
// Here it is ExampleProps and this is ChildrenBuilder
}
}
As you can see, in ExampleDecorator ExampleProps is not in "this" but in "it". How can I change this to create same receiver type as React?
t
turansky
01/20/2023, 5:32 PM
First rule of
ChildrenBuilder
- don’t use
ChildrenBuilder
directly 🙂
r
Rafał Kuźmiński
01/20/2023, 5:56 PM
Is there a reason? And how in other way can I create such a decorator?
t
turansky
01/20/2023, 10:43 PM
If it’s only about mandatory fields - it’s recommended to use default component creation instead
r
Rafał Kuźmiński
01/23/2023, 9:03 AM
What do you mean by default component creation? Can you provide some example with mandatory fields?