This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-01-09
Channels
- # announcements (9)
- # babashka (14)
- # beginners (27)
- # biff (4)
- # calva (3)
- # cider (14)
- # clojure (36)
- # clojure-austin (1)
- # clojure-europe (43)
- # clojure-japan (4)
- # clojure-nl (2)
- # clojure-norway (59)
- # clojure-uk (6)
- # clojurescript (13)
- # conjure (2)
- # data-science (3)
- # datomic (3)
- # deps-new (40)
- # hyperfiddle (72)
- # jobs (2)
- # lsp (8)
- # malli (10)
- # missionary (3)
- # off-topic (22)
- # overtone (3)
- # reagent (12)
- # releases (1)
- # squint (1)
I'm using the same component for both login & sign-in (register) puproses, and want to clear local state & reset (cleanup) the form only if input arguments were changed (not on local state change). My code below works, but isn't it a dirty hack? Or it's regular way to use the implicit side effects on component render?
(defn- register-login [register?]
(r/with-let [state (r/atom nil)
old-argv (atom register?)
ref (atom nil)]
;; emulation of :component-did-update
(do
(when (and (not= register? @old-argv)
@ref)
(reset! state nil)
(.reset @ref))
(reset! old-argv register?))
[:form.main-header
{:ref #(reset! ref %)
:on-submit (fn [e]
................
Sometimes you do have to use something like that. A cleaner approach would be to use a form-3 component and check that the only property has changed in :component-did-update
.
However, in your case I'd probably extract all the common parts into their own vars and use those to create separate form components - one for logging in and another for signing up.
Finished with 2 small components with different states while core is stateless form-1 component
(defn- register-page []
(r/with-let [state (r/atom nil)]
[register-login-core true state]))
(defn- login-page []
(r/with-let [state (r/atom nil)]
[register-login-core false state]))
Nice. FWIW, I usually suffix the names of such components with -impl
instead of -core
.
Ok. Btw, should I call that -impl as component or as regular function in round parens (register-login-core false state)
?
Doesn't matter that much when register-login-core
doesn't have any state on its own.
Hello
I'm working a big reagent app that uses r/with-let to manage the state.
The issue was: a component was losing the state during a re-render.
I managed to fix it my replacing a component call [my-wired-table ...]
with a function call (my-wired-table ...)
I'd like to know where can I read more about reagent implementation and the implications of []
vs ()
, or even the internals of r/with-let.
Usually it's the opposite - using ()
instead of []
can lead to a lost local state.
[]
creates a new component from the passed function, ()
just calls your function, that's it.
I don't think r/with-let
is documented to the fullest extent anywhere, so I'd suggest studying its implementation.
If you can create an MRE, I'd be curious to take a look.