Created
October 28, 2018 04:58
-
-
Save cristianoc/af470092dc153e82d26ba87541cf9fec to your computer and use it in GitHub Desktop.
Essence of Hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let foo = (~name, ~surname, _) => { | |
print_string("name: " ++ name); | |
print_string("surname: " ++ surname); | |
}; | |
module State: { | |
type t('a); | |
let init: 'a => t('a); | |
let get: t('a) => 'a; | |
let set: (t('a), 'a) => unit; | |
} = { | |
type t('a) = ref('a); | |
let init = (s: 'a): t('a) => ref(s); | |
let get = x => x^; | |
let set = (x, y) => x := y; | |
}; | |
module Event = { | |
let onClick = f => | |
if (Random.bool()) { | |
let choices = [|"some", "random", "words"|]; | |
let n = choices->Array.length; | |
choices[Random.int(n)]->f; | |
}; | |
}; | |
let foo2 = | |
( | |
~name, | |
~stateName=State.init(name), | |
~surname, | |
~stateSurname=State.init(surname), | |
_, | |
) => { | |
Event.onClick(s => stateName->(State.set(s))); | |
Event.onClick(s => stateSurname->(State.set(s))); | |
print_string("name: " ++ stateName->State.get); | |
print_string("surname: " ++ stateSurname->State.get); | |
}; | |
let main = () => foo(~name="Mary", ()); | |
let main2 = () => foo2(~name="Mary", ()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment