Fork me on GitHub
#re-frame
<
2018-05-25
>
Bravi16:05:18

Hi everyone. I’ve got this component

(defn project-view
  [defaults]
  (let [form-state (r/atom defaults)]
    (fn [project]
      [:div.form-group]
      ;; and a bunch of stuff
      )))
so I put all the defaults in that form-state and then use that atom to update things in the form. once I click on submit, I’m sending a post request to the server and it returns back an updated model. So what I want to do is to replace those defaults with the updated model basically. So this is what I came up with and was wondering if there’s another way of doing it
(defn project-view
  [defaults]
  (let [original   (r/atom defaults)
        form-state (r/atom defaults)]
    (fn [project]
      (let [state    @form-state
            defaults @original]
        (when (not= defaults project)
          (reset! original project)
          (reset! form-state project))
        [:div.form-group]
        ;; and a bunch of stuff
        ))))

achikin16:05:33

That's more a #reagent question. The reason defaults are not updated is that your (let [form-state (r/atom defaults)]) only called once upon component creation and is never called again.

achikin16:05:28

The way you've managed it is ok.

Bravi16:05:39

yeah, I know but I was wondering if this approach is alright

Bravi16:05:09

thank you 🙂

achikin16:05:28

The other way to manage it is to keep your form-state in the re-frame database

achikin16:05:38

And update the defaults there.

Bravi16:05:01

yeah I might switch to that actually. I think it’d make it simpler

Bravi17:05:02

ah this is nice! I just refactored the above code and I basically shrunk it down to just

(rf/dispatch [::events/set-form-defaults :update-project project])
  (let [form-state (rf/subscribe [::subs/form-data :update-project])]