I'm compiling code assigning an InputType.file to ...
# react
j
I'm compiling code assigning an InputType.file to a react.dom.html.ReactHTML.input's type, but it's compiling to bad JS; am I doing something wrong?
This is my code:
Copy code
import org.w3c.dom.HTMLInputElement
import org.w3c.files.File
import react.RBuilder
import react.RComponent
import react.PropsWithChildren
import react.State
import react.dom.html.ReactHTML.div
import react.dom.html.ReactHTML.input
import react.setState
import web.html.InputType

class Dump : RComponent<PropsWithChildren, DumpState>() {
    init {
        state.file = null
    }
    override fun RBuilder.render() {
        console.log("blah")
        if (state.file == null) {
            div {
                input {
                    attrs {
                        type = InputType.file
                        onChange = { event ->
                            val file = (event.target as HTMLInputElement).files?.item(0)
                            console.log("got file ", file)
                            setState {
                                this.file = file
                            }
                        }
                    }
                }
            }
        } else {
            child(DumpViewer::class) {
                attrs {
                    file = state.file!!
                }
            }
        }
    }
}

external interface DumpState : State {
    var file: File?
}
And the input's attrs compiles to:
Copy code
function Dump$render$lambda$lambda$lambda(this$0) {
    return function ($this$attrs) {
      // Inline function 'web.html.InputType.Companion.file' call
      InputType;
      $this$attrs.type = 'file';
      $this$attrs.onChange = Dump$render$lambda$lambda$lambda$lambda(this$0);
      return Unit_instance;
    };
  }
which fails at runtime with
Copy code
InputType is not defined
ReferenceError: InputType is not defined
I can get it to work with
type = js("\"file\"")
but that shouldn't be necessary. Am I don't something wrong, or is this a bug somewhere?
Looks like it was a bug - upgrading from Kotlin 1.9.24 to 2.0.0 fixed it.
t
JFYI - if you will use
FC
from
kotlin-react
: 1. You won’t need redundant attrs 2. You will have strict events (with strict
currentTarget
)
j
Is there guidance somewhere on porting from the RComponent idiom to the FC idiom?
t
State properties ->
useState
Class methods ->
useCallback