我的scalajs-react应用程序的代码如下:
def render(person: Person) = {
<.div(
<.p("Welcome!"),
<.form(
<.label("Name:",
<.input(^.`type` := "text", ^.cls := "form-control",
^.value := person.name, ^.onChange ==> updatePersonName
)
))
)
}
def updatePersonName(event: ReactEventFromInput): Callback = {
$.modState(person => person.copy(name = event.target.value))
}
但是我看到错误:
Uncaught TypeError: Cannot read properties of null (reading 'value')
我在这里错过了什么?
使用event.extract
医生说:
如果您想以异步方式访问事件属性(例如:在
modState(…)
函数中)
因此你的updatePersonName
函数应该是:
def updatePersonName(event: ReactEventFromInput): CallbackTo[Unit] = {
event.extract(_.target.value)(text =>
$.modState(person => person.copy(name = text))
)
}