16.9.0-pre.91 • Published 5 years ago

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

Weekly downloads
69
License
Apache-2-0
Repository
github
Last release
5 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

5 years ago

16.9.0-pre.89

6 years ago

16.9.0-pre.83

6 years ago

16.9.0-pre.82

6 years ago

16.8.6-pre.80

6 years ago

16.6.0-pre.67

6 years ago

16.6.0-pre.61

7 years ago

16.6.0-pre.60

7 years ago

16.5.2-pre.58

7 years ago

16.5.2-pre.56

7 years ago

16.5.2-pre.55

7 years ago

16.5.0-pre.54

7 years ago

16.5.0-pre.53

7 years ago

16.5.0-pre.52

7 years ago

16.4.2-pre.49

7 years ago

16.4.2-pre.48

7 years ago

16.4.2-pre.47

7 years ago

16.4.2-pre.46

7 years ago

16.4.2-pre.38

7 years ago

16.4.2-pre.37

7 years ago

16.4.0-pre.31

7 years ago

16.4.0-pre.30

7 years ago

16.3.1-pre.28

7 years ago

16.3.1-pre.27

7 years ago

16.3.1-pre.26

7 years ago

16.3.1-pre.25

7 years ago

16.2.1-pre.23

7 years ago

16.2.1-pre.22

7 years ago

16.2.1-pre.21

7 years ago

16.2.1-pre.20

7 years ago

16.2.1-pre.19

7 years ago

16.2.1-pre.18

7 years ago

16.2.1-pre.17

7 years ago

16.2.1-pre.15

7 years ago

16.2.1-pre.13

7 years ago

16.2.1-pre.12

7 years ago

16.2.1-pre.11

7 years ago

16.2.0-pre.20

7 years ago

16.2.0-pre.16

8 years ago

16.2.0-pre.15

8 years ago

16.2.0-pre.14

8 years ago

16.0.0-pre.13

8 years ago

16.0.0-pre.12

8 years ago

16.0.0-pre.11

8 years ago

16.0.0-pre.10

8 years ago

16.0.0-pre.7

8 years ago

16.0.0-pre.6

8 years ago

16.0.0-pre.5

8 years ago

16.0.0-pre.4

8 years ago

16.0.0-pre.3

8 years ago

16.0.0-pre.2

8 years ago

16.0.0-pre.1

8 years ago

16.0.0-pre.0

8 years ago

16.0.0-pre

8 years ago