Coding based FAQs - fxfactorial/ocaml-reactjs GitHub Wiki

  • How can I set the type attribute of an input element?
DOM.make ~tag:`input ~elem_spec:(object%js
                           val type = "checkbox"
                           val className = "toggle"
                         end)
                        [])

Answer: Because of the naming conflict with OCaml type keyword we get around it by making the object this way:

~elem_spec:([("type", Js.string "checkbox"); ("className", Js.string "toggle")] >>> object%js end)

This takes the object on the right and mutates it with the items on the left. >>> comes from ReactJs.Infix

You can also get around it by appending a _ to the value type. This is a trick employed by the ppx provided by js_of_ocaml. Snippet from the quadratic example:


Elem (make ~elem_spec:(object%js
        val type_ = !*"number"
        val value = !^init_value
        val onChange = handle_input_change ~key 
      end) ~tag:`input [])