16.9.0-pre.91 • Published 4 years ago

@jetbrains/kotlin-react v16.9.0-pre.91

Weekly downloads
69
License
Apache-2-0
Repository
github
Last release
4 years ago

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.

16.9.0-pre.91

4 years ago

16.9.0-pre.89

4 years ago

16.9.0-pre.83

5 years ago

16.9.0-pre.82

5 years ago

16.8.6-pre.80

5 years ago

16.6.0-pre.67

5 years ago

16.6.0-pre.61

5 years ago

16.6.0-pre.60

5 years ago

16.5.2-pre.58

6 years ago

16.5.2-pre.56

6 years ago

16.5.2-pre.55

6 years ago

16.5.0-pre.54

6 years ago

16.5.0-pre.53

6 years ago

16.5.0-pre.52

6 years ago

16.4.2-pre.49

6 years ago

16.4.2-pre.48

6 years ago

16.4.2-pre.47

6 years ago

16.4.2-pre.46

6 years ago

16.4.2-pre.38

6 years ago

16.4.2-pre.37

6 years ago

16.4.0-pre.31

6 years ago

16.4.0-pre.30

6 years ago

16.3.1-pre.28

6 years ago

16.3.1-pre.27

6 years ago

16.3.1-pre.26

6 years ago

16.3.1-pre.25

6 years ago

16.2.1-pre.23

6 years ago

16.2.1-pre.22

6 years ago

16.2.1-pre.21

6 years ago

16.2.1-pre.20

6 years ago

16.2.1-pre.19

6 years ago

16.2.1-pre.18

6 years ago

16.2.1-pre.17

6 years ago

16.2.1-pre.15

6 years ago

16.2.1-pre.13

6 years ago

16.2.1-pre.12

6 years ago

16.2.1-pre.11

6 years ago

16.2.0-pre.20

6 years ago

16.2.0-pre.16

6 years ago

16.2.0-pre.15

6 years ago

16.2.0-pre.14

6 years ago

16.0.0-pre.13

6 years ago

16.0.0-pre.12

6 years ago

16.0.0-pre.11

6 years ago

16.0.0-pre.10

6 years ago

16.0.0-pre.7

7 years ago

16.0.0-pre.6

7 years ago

16.0.0-pre.5

7 years ago

16.0.0-pre.4

7 years ago

16.0.0-pre.3

7 years ago

16.0.0-pre.2

7 years ago

16.0.0-pre.1

7 years ago

16.0.0-pre.0

7 years ago

16.0.0-pre

7 years ago