re-frame 2024-11-25

hi o/ I'm getting strange issues with using re-frame and reagent form-2 components and they seem to be falling apart in pretty random ways. Just checking if I'm doing something stupid. Tried this:

(defn login-name []
  (let [!input-name (atom nil)
        send-code #(rf/dispatch [:login/send-otp-code (-> @!input-name .-value)])
        loading? @(rf/subscribe [:login/loading?])
        error @(rf/subscribe [:login/error])]
    (fn []
      [...])))
and the state gets widely desynchronized. This however, seems to always work:
(defn login-name []
  (let [!input-name (atom nil)
        send-code #(rf/dispatch [:login/send-otp-code (-> @!input-name .-value)])]
    (fn []
      (let [loading? @(rf/subscribe [:login/loading?])
            error @(rf/subscribe [:login/error])]
        [...]))))
The first approach seems to work sometimes if I move the deref into the function. But again, is wobbly. Is that something I shouldn't do? All examples I've found use form-1 components, but I need the :ref binding in this component and it seems wrong to run that one through re-frame.

✅ 1

Don't deref the subs in the outer fn, deref them in the inner fn. So definitely the second code block. So you can either go with the second code block (something I would do myself), or you can subscribe in that outer let and @ in the inner let.

thanks. I tried @ in the inner lets and got strange results and never figured where my state falls apart and the second version always works so I'm happy with a double-let. Was just having a sanity-check because I haven't seen people do that 🙂

The only difference between subscribing in the outer fn and in the inner fn might come from passing arguments to subscriptions. But your code samples don't do that.