Hi I am relatively new to Kotlin-React. I try to u...
# react
r
Hi I am relatively new to Kotlin-React. I try to use the react-grid-layout (https://github.com/STRML/react-grid-layout) with Kotlin. Therefore I made the following interface file:
Copy code
@file:JsModule("react-grid-layout")
@file:JsNonModule

import kotlinext.js.Object
import react.RClass
import react.RProps

@JsName("default")
external val ReactGridLayout: RClass<GridLayoutProps>

external interface GridLayoutProps: RProps {
    var autoSize: Boolean
    var cols: Int
    var calssName: String
    var style: Object
    var draggableHandle: String
    var draggableCancel: String
    var containerPadding: Any
    var rowHeight: Int
    var maxRows: Int
    var layout: Array<IntArray>
    var margin: Array<Int>
    var isDraggable: Boolean
    var isResizable: Boolean
    var isDroppable: Boolean
    var useCSSTransforms: Boolean
    var transformScale: Int
    var verticalCompact: Boolean
    var compactType: String?
    var preventCollision: Boolean
    var droppingItem: Object
    var onLayoutChange: Any
    var onDragStart: Any
    var onDrag: Any
    var onDragStop: Any
    var onResizeStart: Any
    var onResize: Any
    var onResizeStop: Any
    var onDrop: Any
}
When I now try to use this, I only get errors. What am I doing wrong?
g
what errors do you get?
r
I get the following errors and warnings:
Copy code
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `App`.
    in App
Copy code
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `App`.
Copy code
The above error occurred in the <App> component:
    in App

Consider adding an error boundary to your tree to customize error handling behavior.
Visit <https://fb.me/react-error-boundaries> to learn more about error boundaries.
Copy code
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `App`.
g
You could try doing
val reactGridLayout = require("react-grid-layout")
and console.log it to see what it prints
r
OK? When I try
require("react-grid-layout")
I get a Type mismatch Required Boolean, Found String
g
use the require() function from the kotlin.js library
you probably imported the other require function
r
when I looked closer on the examples, I found out that they don’t use ReactGridLayout directly, they use it like this:
Copy code
import RGL, { WidthProvider } from "react-grid-layout";

const ReactGridLayout = WidthProvider(RGL);
How can I achieve this in Kotlin?
g
I believe
val reactGridLayout = require("react-grid-layout") as ReactGridLayout
should work
As for the WidthProvider,
@JsName("WidthProvider")
on top of the WidthProvider class declaration should do
r
OK, will give it a try
thanks
g
the require(module: String) function is located in kotlinext.js package
r
I now have figured out how it works:
Copy code
val RGL = kotlinext.js.require("react-grid-layout")
val WidthProvider = kotlinext.js.require("react-grid-layout").WidthProvider
val ReactGridLayout: RClass<ReactGridLayoutProps> = WidthProvider(RGL) as RClass<ReactGridLayoutProps>
Thanks for your help :-)
🙂 1
🔥 1