@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.9115latest

Version History

VersionDownloads (Last 7 Days)Published
16.9.0-pre.9115
16.9.0-pre.890
16.9.0-pre.830
16.9.0-pre.820
16.8.6-pre.800
16.6.0-pre.670
16.6.0-pre.610
16.6.0-pre.600
16.5.2-pre.580
16.5.2-pre.560
16.5.2-pre.550
16.5.0-pre.540
16.5.0-pre.530
16.5.0-pre.520
16.4.2-pre.490
16.4.2-pre.480
16.4.2-pre.470
16.4.2-pre.460
16.4.2-pre.380
16.4.2-pre.370
16.4.0-pre.310
16.4.0-pre.300
16.3.1-pre.280
16.3.1-pre.270
16.3.1-pre.260
16.3.1-pre.250
16.2.1-pre.230
16.2.1-pre.220
16.2.1-pre.210
16.2.1-pre.200
16.2.1-pre.190
16.2.1-pre.180
16.2.1-pre.170
16.2.1-pre.150
16.2.1-pre.130
16.2.1-pre.120
16.2.1-pre.110
16.2.0-pre.200
16.2.0-pre.161
16.2.0-pre.150
16.2.0-pre.140
16.0.0-pre.130
16.0.0-pre.120
16.0.0-pre.110
16.0.0-pre.100
16.0.0-pre.70
16.0.0-pre.61
16.0.0-pre.50
16.0.0-pre.40
16.0.0-pre.30
16.0.0-pre.20
16.0.0-pre.10
16.0.0-pre.00
16.0.0-pre0

Package Sidebar

Install

npm i @jetbrains/kotlin-react

Weekly Downloads

17

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