Fork me on GitHub
#re-frame
<
2018-06-15
>
ScArcher21:06:09

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.

eggsyntax21:06:16

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

eggsyntax21:06:30

Whoops, forgot a # on that, editing...fixed.

eggsyntax21:06:03

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.

ScArcher21:06:21

(I'm obviously new)

kennytilton23:06:37

@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…

kennytilton23:06:31

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.

kennytilton23:06:16

Unless of course you want to react to each keystroke, then on-change is fine.

mikerod23:06:37

@hiskennyness you can also implement event “denouncing” with re-frame event handler and effect handler.

kennytilton23:06:14

google…google…google… 🙂

mikerod23:06:17

Just FYI. It can make it less intense by delaying a reaction until the keystrokes have paused for some given window of time

mikerod23:06:37

There are a few re-frame debounce examples out there I believe.

mikerod23:06:55

I ended up implementing my own, but inspired by examples. Can’t recall all details. Will see if I find a link

mikerod23:06:12

I think it is worth looking at the impl and understanding the interaction with the events and effects.

mikerod23:06:23

Gave me more insight into what could be accomplished etc.

kennytilton23:06:43

Drat, I have been “denouncing” FB engineers for a while, you got my hopes up!

kennytilton23:06:36

Thx for the links. My use cases so far just want to wait until the user makes the standard blur/hit enter gestures.

mikerod23:06:48

Sure. That can work in many cases

mikerod23:06:02

And keeps it probably a bit simpler