|
11 | 11 | :once Equivalent to using [] as the deps. |
12 | 12 | :auto-deps Infer the dependencies automatically from the code by finding |
13 | 13 | local vars. Not available for the function form of a hook." |
14 | | - #?(:clj (:require [helix.impl.analyzer :as hana]) |
| 14 | + #?(:clj (:require [helix.impl.analyzer :as hana] |
| 15 | + [helix.impl.hooks :as-alias impl.hooks]) |
15 | 16 | :cljs (:require |
| 17 | + [helix.impl.hooks :as impl.hooks] |
16 | 18 | ["react" :as react] |
17 | 19 | [goog.object :as gobj])) |
18 | 20 | #?(:cljs (:require-macros [helix.hooks]))) |
|
117 | 119 | "Just react/useContext" |
118 | 120 | react/useContext)) |
119 | 121 |
|
120 | | - |
121 | | -;; React `useEffect` expects either a function or undefined to be returned |
122 | | -#?(:cljs |
123 | | - (defn- wrap-fx [f] |
124 | | - (fn wrap-fx-return [] |
125 | | - (let [x (f)] |
126 | | - (if (fn? x) |
127 | | - x |
128 | | - js/undefined))))) |
129 | | - |
130 | 122 | #?(:clj |
131 | 123 | (defn deps-macro-body [env deps body simple-body-ok? deps->hook-body] |
132 | 124 | (cond |
|
182 | 174 | (deps-macro-body |
183 | 175 | &env deps body false |
184 | 176 | (fn |
185 | | - ([fn-body] `^clj-nil (raw-use-effect (wrap-fx (fn [] ~@fn-body)))) |
| 177 | + ([fn-body] `^clj-nil (raw-use-effect (impl.hooks/wrap-fx (fn [] ~@fn-body)))) |
186 | 178 | ([deps fn-body] |
187 | | - `^clj-nil (raw-use-effect (wrap-fx (fn [] ~@fn-body)) ~deps)))))) |
| 179 | + `^clj-nil (raw-use-effect (impl.hooks/wrap-fx (fn [] ~@fn-body)) ~deps)))))) |
188 | 180 |
|
189 | 181 |
|
190 | 182 | #?(:cljs |
|
193 | 185 | ;; be harder to read |
194 | 186 | (defn use-effect* |
195 | 187 | "Like react/useEffect. See `use-effect` for details on what `f`'s return values. See namespace doc for `deps`." |
196 | | - ([f] (react/useEffect (wrap-fx f))) |
| 188 | + ([f] (react/useEffect (impl.hooks/wrap-fx f))) |
197 | 189 | ([f deps] |
198 | 190 | (when goog/DEBUG |
199 | 191 | (when (= deps :auto-deps) |
200 | 192 | (throw (js/Error. "Can't use `:auto-deps` with `use-effect*`; use `use-effect` macro for that")))) |
201 | | - (react/useEffect (wrap-fx f) (to-array deps))))) |
| 193 | + (react/useEffect (impl.hooks/wrap-fx f) (to-array deps))))) |
202 | 194 |
|
203 | 195 |
|
204 | 196 | (defmacro use-layout-effect |
|
209 | 201 | (deps-macro-body |
210 | 202 | &env deps body false |
211 | 203 | (fn |
212 | | - ([fn-body] `^clj-nil (raw-use-layout-effect (wrap-fx (fn [] ~@fn-body)))) |
| 204 | + ([fn-body] `^clj-nil (raw-use-layout-effect (impl.hooks/wrap-fx (fn [] ~@fn-body)))) |
213 | 205 | ([deps fn-body] |
214 | | - `^clj-nil (raw-use-layout-effect (wrap-fx (fn [] ~@fn-body)) ~deps)))))) |
| 206 | + `^clj-nil (raw-use-layout-effect (impl.hooks/wrap-fx (fn [] ~@fn-body)) ~deps)))))) |
215 | 207 |
|
216 | 208 |
|
217 | 209 | #?(:cljs |
218 | 210 | (defn use-layout-effect* |
219 | 211 | "Like `use-effect*` but instead calls react/useLayoutEffect." |
220 | | - ([f] (react/useLayoutEffect (wrap-fx f))) |
| 212 | + ([f] (react/useLayoutEffect (impl.hooks/wrap-fx f))) |
221 | 213 | ([f deps] |
222 | 214 | (when goog/DEBUG |
223 | 215 | (when (= deps :auto-deps) |
224 | 216 | (throw (js/Error. "Can't use `:auto-deps` with `use-layout-effect*`; use `use-layout-effect` macro for that")))) |
225 | | - (react/useLayoutEffect (wrap-fx f) (to-array deps))))) |
| 217 | + (react/useLayoutEffect (impl.hooks/wrap-fx f) (to-array deps))))) |
226 | 218 |
|
227 | 219 |
|
228 | 220 | (defmacro use-memo |
|
0 commit comments