How to fix this type error?

@react.component
let make = (~children) => {
  let handleClick = (e) => {
    Js.log(e["target"])
  }
  <button onClick={e => handleClick(e)}> children </button>
}

  • e has the type ReactEvent.Mouse.t
  • You are handling e as if it was an open Js.t type {.. "target": 'a}

To solve this, you need to first use ReactEvent.Mouse.target to convert the event into an open Js.t:

@react.component
let make = (~children) => {
  let handleClick = (e) => {
    ReactEvent.Mouse.target(e)->Js.log
  }
  <button onClick={e => handleClick(e)}> children </button>
}

Also see Playground Example.

1 Like