Fork me on GitHub

Hi everyone. I’ve got this component

(defn project-view
  (let [form-state (r/atom defaults)]
    (fn [project]
      ;; 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
  (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))
        ;; and a bunch of stuff


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.


The way you've managed it is ok.


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


thank you 🙂


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


And update the defaults there.


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


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])]