Overloadable React components
See original GitHub issueJSX allows us to pass either string or another JSX element into attribute. Both options bellow are valid
<Container header="My header" />
<Container header={<h1>My header</h1>} />
I wanted to achieve the same effect in the Kotlin code:
Container(header = "My header")
Container(header = { h1 { +"My header" }})
I managed to make it work via two overloaded functions
fun RBuilder.Container(header: RBuilder.() -> Unit) {
//..
}
fun RBuilder.Container(header: String) {
//..
}
It works, but if I have more than one property, I will have to create bigger number of functions
fun RBuilder.Container(header: RBuilder.() -> Unit, content: RBuilder.() -> Unit)
fun RBuilder.Container(header: String, content: RBuilder.() -> Unit)
fun RBuilder.Container(header: RBuilder.() -> Unit, content: String)
fun RBuilder.Container(header: String, content: String)
For 3 different properties there will be 9 functions.
Is there a way to simplify this and create only one signature that accepts both String and RBuilder types?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to Overload TypeScript React Component Interfaces ...
This article explains how to create correct, intuitive APIs for React components in TypeScript by using prop values to overload React component interfaces....
Read more >Function overloading with typescript on a react component?
You don't need to overload your component in this case. COnsider this example: import React from 'react' type Boy = { color: string...
Read more >React Anti-Pattern: Overloaded
React Anti-Pattern: Overloaded · A function that outputs content to the console (submit feature) · A form consisting of two fields and a...
Read more >react-component-overloading
VS Code's tsserver was deleted by another application such as a misbehaving virus detection tool. Please reinstall VS Code. Manage Extension.
Read more >TypeScript React function Component defined as an ...
TypeScript React function Component defined as an overloaded function type generates incorrect prop types. 1.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Well, there’s https://github.com/square/kotlinpoet
I personally would stick with
fun RBuilder.Container(header: RBuilder.() -> Unit, content: RBuilder.() -> Unit)
as the most flexible method and drop the rest.@Hypnosphi, @Leonya thank you for the kind answers! Will try to come up with some solution.