【问题标题】:Scalajs-react: Uncaught TypeError: Cannot read properties of null (reading 'value')Scalajs-react:未捕获的 TypeError:无法读取 null 的属性(读取“值”)
【发布时间】:2021-11-22 20:01:28
【问题描述】:

我的 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')

我在这里错过了什么?

【问题讨论】:

    标签: javascript reactjs scala.js scalajs-react


    【解决方案1】:

    你已经使用event.extract

    文档说:

    如果您想以异步方式访问事件属性(例如在modState(…) 函数中)

    因此,您的 updatePersonName 函数应该如下所示:

    def updatePersonName(event: ReactEventFromInput): CallbackTo[Unit] = {
      event.extract(_.target.value)(text =>
        $.modState(person => person.copy(name = text))
      )
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-20
      • 2022-11-24
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 2022-11-21
      相关资源
      最近更新 更多