kotlin-wrappers icon indicating copy to clipboard operation
kotlin-wrappers copied to clipboard

Documentation for kotlin-react

Open hofi99 opened this issue 3 years ago • 10 comments

Migrating kotlin-react from a lower version than pre.282 is currently quite difficult due to the lack of documentation on the new kotlin-react and also kotlin-react-dom versions with the ChildrenBuilder() and its other changes. It would be really helpful to have a demo project with functional components and especially class components as I haven't seen an updated project using these.

Currently having just the changelog doesn't seem to be sufficient for a smooth migration process. An introduction like the one from kotlin-react-legacy would help immensely in that regard.

hofi99 avatar Feb 25 '22 16:02 hofi99

Demo projects:

  • https://github.com/karakum-team/kotlin-react-table-sample
  • https://github.com/karakum-team/kotlin-mui-showcase

cc @SebastianAigner

turansky avatar Feb 25 '22 16:02 turansky

The examples above don't include any class components. There is still a few situations where writing a class component is more straightforward than using hooks so it would be nice to still be able to use them. It seems the new code only has a builder that is very much locked down to be used only from functional components. Might help to loosen up the restrictions a bit. Currently I can get a class component in the new code, using the new builder, by writing something rather hacky like this:

data class ThingState(var click: Boolean = false):State
class Thing : Component<Props, ThingState>() {
    init {
        state =ThingState() 
    }
    companion object{
    private val render =FC<Props> {
        button {
            +"Click Me"
            onClick = {
                setState(state.copy { click = !state.click })
            }
        }
        div {
            +state.click.toString()
        }
     }
   }
    override fun render(): ReactNode = render.create()
}

The obvious side effect of this is that if I look at React DevTools I see a Thing component followed by an Anonymous component (or whatever name I give to the inner functional component). There are potentially other ways to avoid that but they likely would rely on accessing the children of the builder which is also inaccessible.

piacenti avatar Jun 17 '22 15:06 piacenti

@piacenti Have a look at our project CampusQR where we use class components with the new kotlin wrappers.

hofi99 avatar Jun 21 '22 07:06 hofi99

Looks good and looks similar to how the old wrapper is setup. I think that should be included in the new dsl so that people don't have to reimplement that same code in every project. Good use of fragment by the way, it would definitely makes my hack above much cleaner and not much of a hack at all since there would be no need for a nested FC

piacenti avatar Jun 22 '22 06:06 piacenti

I was thinking the same but apparently they want to push people towards using function components. Not perfect to have this code in every project, but since it only changes very rarely I guess it's not that big of a problem for us

hofi99 avatar Jun 23 '22 07:06 hofi99

I'm trying to replicate from the examples listed here, but it seems some big changes happened to the API: Screenshot_20220916_214811

Martmists-GH avatar Sep 16 '22 19:09 Martmists-GH

@Martmists-GH Make sure you have the function available. There is an FC function and FC interface. They share the same name and are in the same package. The interface is in the kotlin-react-core module while the function is in the kotlin-react module

fun <P : Props> FC(
    block: ChildrenBuilder.(props: P) -> Unit,
): FC<P> 
external interface FC<in P : Props> :
    ComponentType<P>

piacenti avatar Nov 19 '22 03:11 piacenti

Sort of related to documentation - I'm just getting into kotlin-emotion w/React & started to work this up:

https://github.com/santansarah/kotlin-wrappers/tree/master/kotlin-emotion

This is a work-in-progress; just a concept really. Would this be useful? Is this the right spot for it? If there's interest for me to keep going, let me know...or if you have ideas, things that should be included, etc...

santansarah avatar Mar 21 '23 22:03 santansarah

Love the initiative since it lowers the entry barrier! @santansarah

hofi99 avatar Mar 22 '23 07:03 hofi99

Cool, thanks @hofi99. I made some updates & I'll continue to work on it in my free time. To all - feel free to make edits; comments, especially since I'm still getting familiar with the library.

santansarah avatar Mar 22 '23 20:03 santansarah