Skip to content

Instantly share code, notes, and snippets.

@adz5a
Created September 22, 2019 12:48
Show Gist options
  • Save adz5a/82bf48ad71ea069786d2171e742dcc43 to your computer and use it in GitHub Desktop.
Save adz5a/82bf48ad71ea069786d2171e742dcc43 to your computer and use it in GitHub Desktop.
(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