Created
September 22, 2019 12:48
-
-
Save adz5a/82bf48ad71ea069786d2171e742dcc43 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(defn use-atom | |
"Creates a subscription to the given atom and will put its state inside a setState. | |
Returns the current state." | |
[a] | |
(let [[state set-state] (use-state @a)] | |
(react/useEffect | |
(fn [] | |
(let [k (-> gensym str keyword)] | |
(add-watch a k (fn [k r old-state new-state] | |
(set-state new-state))) | |
(fn [] | |
(remove-watch a k)))) | |
#js []) | |
state)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment