@jetbrains/kotlin-react

16.9.0-pre.91 • Public • Published

kotlin-react

Kotlin wrapper for React library. Major version number of this wrapper matches that of React itself.

Installation

  1. npm i @jetbrains/kotlin-react

  2. npm run gen-idea-libs

See the Bintray page for Maven and Gradle installation instructions.

Creating a simple React component with Kotlin

As you might know, the simplest way to define a React component in JavaScript is to write a function. Like this:

import React from 'react';

export function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

Here's what the roughly equivalent Kotlin code looks like:

package hello

import react.*
import react.dom.*

fun RBuilder.hello(name: String) {
    h1 {
        +"Hello, $name"
    }
}

RBuilder lets you construct your component's markup using type-safe builders, similarly to JSX.

When writing React code in JavaScript the type annotations for props (via PropTypes) are optional, but in Kotlin they are not.

Here's an example of a component defined using a class with a name property of type String:

package welcome

import react.*
import react.dom.*

interface WelcomeProps: RProps {
    var name: String
}

class Welcome: RComponent<WelcomeProps, RState>() {
     override fun RBuilder.render() {
        div {
            +"Hello, ${props.name}"
        }
    }
}

fun RBuilder.welcome(name: String = "John") = child(Welcome::class) {
    attrs.name = name
}

And here's how we can use this component in another component:

import welcome.*

fun RBuilder.app {
    welcome("Jane")
}

Type-safe inline styles

There is no built-in capability for writing inline styles in a type-safe manner. However, it can be done by adding a dependency on kotlin-css and a simple utility function.

var Tag.style: RuleSet
    get() = error("style cannot be read from props")
    set(value) = jsStyle {
        CSSBuilder().apply(value).declarations.forEach {
            this[it.key] = when (it.value) {
                !is String, !is Number -> it.value.toString()
                else -> it.value
            }
        }
    }
    
fun Tag.style(handler: RuleSet) {
    style = handler
}

Declaring static fields and lifecycle methods (contextType, getDerivedStateFromProps(), etc.)

There is currently no easy way to declare static members from Kotlin/JS (see KT-18891), so please do the following instead:

class MyComponent: RComponent<MyComponentProps, MyComponentState>() {
    companion object : RStatics<MyComponentProps, MyComponentState, MyComponent, Nothing>(MyComponent::class) {
        init {
            getDerivedStateFromProps = { props, state ->
                // ...
            }
        }
    }
}

Internals

Imports.kt contains type definitions for React. The remaining classes (React.kt and others) provide higher-level APIs on top of that definition.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
16.9.0-pre.916latest

Version History

VersionDownloads (Last 7 Days)Published
16.9.0-pre.916
16.9.0-pre.891
16.9.0-pre.831
16.9.0-pre.821
16.8.6-pre.801
16.6.0-pre.671
16.6.0-pre.611
16.6.0-pre.601
16.5.2-pre.581
16.5.2-pre.561
16.5.2-pre.551
16.5.0-pre.541
16.5.0-pre.531
16.5.0-pre.521
16.4.2-pre.491
16.4.2-pre.481
16.4.2-pre.471
16.4.2-pre.461
16.4.2-pre.381
16.4.2-pre.371
16.4.0-pre.311
16.4.0-pre.301
16.3.1-pre.281
16.3.1-pre.271
16.3.1-pre.261
16.3.1-pre.251
16.2.1-pre.231
16.2.1-pre.221
16.2.1-pre.211
16.2.1-pre.201
16.2.1-pre.191
16.2.1-pre.181
16.2.1-pre.171
16.2.1-pre.151
16.2.1-pre.131
16.2.1-pre.121
16.2.1-pre.111
16.2.0-pre.201
16.2.0-pre.161
16.2.0-pre.151
16.2.0-pre.141
16.0.0-pre.131
16.0.0-pre.121
16.0.0-pre.111
16.0.0-pre.101
16.0.0-pre.71
16.0.0-pre.61
16.0.0-pre.51
16.0.0-pre.41
16.0.0-pre.31
16.0.0-pre.21
16.0.0-pre.11
16.0.0-pre.01
16.0.0-pre1

Package Sidebar

Install

npm i @jetbrains/kotlin-react

Weekly Downloads

59

Version

16.9.0-pre.91

License

Apache-2-0

Unpacked Size

601 kB

Total Files

6

Last publish

Collaborators

  • skoch13
  • jetbrains-admin
  • kotlin
  • jetbrains-buildserver
  • allvo
  • bashor