luciodale / fork

A non-intrusive Clojurescript form management library for Re-frame and Reagent.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to make form state survive hot-reload?

jtlocsei opened this issue · comments

Is there a way to make the form inputs survive Shadow-cljs hot-reload?

For instance, suppose I have a textarea that I've typed something into in my app. Then I edit the form styling and save the cljs file. Shadow-cljs hot-reloads the app to show the new form styling, but the textarea input is reset to blank. I'd like the textarea to keep whatever I'd typed in it. Is this possible?

Thanks in advance for any help!

This is not really an issue with fork but with the way reagent works.

A workaround would be to define a ratom in your file with (defonce my-state (r/atom {})) and pass this value to work under the :state key.

In this way you won’t lose any of your values when shadows hot reloads the view :) let me know

Thanks for the speedy reply and suggestion! Using :state sounds promising. I just tried it though and the form state is lost on hot reload. This is what I tried:

(defonce form-state (r/atom {}))

(defn my-form
  [] 
  [fork/form
   {:state form-state
    :keywordize-keys true}
   (fn [{:keys [values handle-change handle-blur]}]
     [:form
      [:div.field
       [:label.label "Enter message"]
       [:div.control
        [:textarea.textarea
         {:name "my-textarea"
          :value (:my-textarea values)
          :on-change handle-change
          :on-blur handle-blur}]]]])])

I think maybe the state is lost on hot-reload because core.cljs/initialize-state doesn't check for any existing :values in the passed in state?

That's correct. If you bump to v2.4.3 the problem is solved, and your values are no longer overwritten.

Awesome, thanks so much! Just tried it and it works great :-)