This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-06-15
Channels
Hey, #re-frame is fun! https://github.com/kennytilton/rehiring/blob/master/README.md
In re-frame, how do I update the database with a value in a form field. I'd like the database value to change whenever the form field value changes.
I'm relatively new to modern re-frame myself, but here's how I'm doing it:
(rf/reg-event-db
:register-input
(fn-traced [db [_ cur-input]]
(assoc db :input-storage-key cur-input)))
and then in the view:
[:input {:type "text"
:on-change #(rf/dispatch [:register-input (.-value (.-target %))])}]
Note that that'll update app-db
for every keystroke -- depending on your use case, that may be more noise in event history than you want. You could implement some debouncing to decrease the noise.
@scott.archer Warning: I am new, too. I would have a handler (rfr/dispatch {:ff-set :ff-name value])
, then have a (reg-event-db :ff-set etc) simply (assoc-in db [:path-to-fields ff-name] value)
. But…
If you use on-change
, it will fire on each keystroke, because React is a bit nutty. I have been duplicating the handling in :on-blur and :on-key-press, checking for the enter key on the latter.
Unless of course you want to react to each keystroke, then on-change is fine.
@hiskennyness you can also implement event “denouncing” with re-frame event handler and effect handler.
google…google…google… 🙂
Just FYI. It can make it less intense by delaying a reaction until the keystrokes have paused for some given window of time
I ended up implementing my own, but inspired by examples. Can’t recall all details. Will see if I find a link
I think I drew inspiration from https://github.com/johnswanson/re-frame-debounce-fx/blob/develop/README.md
I think it is worth looking at the impl and understanding the interaction with the events and effects.
Drat, I have been “denouncing” FB engineers for a while, you got my hopes up!
Thx for the links. My use cases so far just want to wait until the user makes the standard blur/hit enter gestures.