-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.cljs
39 lines (32 loc) · 990 Bytes
/
core.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(ns rehook.core
(:require ["react" :as react]))
(defn use-state
[initial-value]
(react/useState initial-value))
(defn use-effect
([f]
(react/useEffect f))
([f deps]
(react/useEffect f (to-array deps))))
(defn use-atom-fn
[a getter-fn setter-fn]
(let [[val set-val] (use-state (getter-fn @a))]
(use-effect
(fn []
(let [id (str (random-uuid))]
(add-watch a id (fn [_ _ prev-state next-state]
(let [prev-value (getter-fn prev-state)
next-value (getter-fn next-state)]
(when-not (= prev-value next-value)
(set-val next-value)))))
#(remove-watch a id)))
[])
[val #(swap! a setter-fn %)]))
(defn use-atom
"(use-atom my-atom)"
[a]
(use-atom-fn a identity (fn [_ v] v)))
(defn use-atom-path
"(use-atom my-atom [:path :to :data])"
[a path]
(use-atom-fn a #(get-in % path) #(assoc-in %1 path %2)))