Skip to content

Instantly share code, notes, and snippets.

@patricoferris
Last active January 23, 2021 14:10
Show Gist options
  • Save patricoferris/2e24fb51580257e626b981346facfe24 to your computer and use it in GitHub Desktop.
Save patricoferris/2e24fb51580257e626b981346facfe24 to your computer and use it in GitHub Desktop.
State management with Javascript objects
open Mithril
open Brr
module State = struct
type t = Jv.t
let state = Jv.obj [| ("id", Jv.of_int 0) |]
let get_id () = Jv.get state "id" |> Jv.to_int
let set_id v = Jv.set state "id" (Jv.of_int v)
end
let () =
let body = Document.body G.document in
let attr =
Attr.(
v [| attr "onclick" (Jv.repr (fun _ -> State.(set_id (get_id () + 1)))) |])
in
let button =
let view _ =
M.v "button" ~attr ~children:(`String (string_of_int @@ State.get_id ()))
in
Component.v view
in
M.mount body button
@patricoferris
Copy link
Author

Compile with:

(executable
 (name index)
 (modes js)
 (libraries js_of_ocaml brr mithril))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment