Skip to content

Instantly share code, notes, and snippets.

@Et7f3
Created April 22, 2019 17:12
Show Gist options
  • Save Et7f3/c8d85b00c352fa9b15316e8760940f39 to your computer and use it in GitHub Desktop.
Save Et7f3/c8d85b00c352fa9b15316e8760940f39 to your computer and use it in GitHub Desktop.
test for React.Hook
open Revery
open Revery.UI
open Revery.UI.Components
type main_view_state =
{
input: string;
}
module MainView = struct
let component =
React.component (("MainView")[@reason.raw_literal "MainView"])
type action =
Empty
| NewVal of string
| Compute
let reducer action state =
match action with
Empty ->
let () = print_endline ("We empty. Old value: " ^ state.input) in
{input = ""}
| NewVal input -> {input}
| Compute ->
let () = print_endline ("We compute " ^ state.input) in
{input = ""}
let createElement ~children:_ () =
component
(fun hooks ->
let ({input}, dispatch,hooks) =
React.Hooks.reducer ~initialState:{input = ""} reducer hooks
in
(hooks, View.createElement ~children:[
Input.createElement ~value:input ~placeholder:input ~onChange:(fun {value; _} -> dispatch(NewVal value)) ~children:[] ();
Button.createElement ~title:"Compute" ~onClick:(fun _ -> dispatch Compute) ~children:[] ();
Button.createElement ~title:"Empty" ~onClick:(fun _ -> dispatch Empty) ~children:[] ()
] ()))
end
let init app =
let win =
App.createWindow app (("OCalc")[@reason.raw_literal "OCalc"])
in
let render () = MainView.createElement ~children:[] () in
UI.start win render
let _ = App.start init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment