question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Overloadable React components

See original GitHub issue

JSX 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Leonyacommented, Nov 8, 2017

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.

0reactions
just-boriscommented, Nov 8, 2017

@Hypnosphi, @Leonya thank you for the kind answers! Will try to come up with some solution.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found