Fork me on GitHub
#re-frame
<
2020-05-07
>
solf06:05:22

If I understood correctly, in a pure reagent app with a single r/atom, any component dereferencing the ratom would be redraw every time any part of the ratom is modified. When using rf/subscribe, does it still work that way? If I have a component that subscribes to something that returns (:foo db) and I change db :bar, is the component re-drawn?

ingesol06:05:26

@dromar56 It should work the same way. If the return value of the subscription changes, component will redraw

sylvain09:05:28

I'm using a React component that expects a callback returning a Promise as one of its properties. I implemented it like this:

(fn [data]
  (js/Promise. (fn [resolve reject]
                 (letfn [(cb [[event args] queue]
                           (cond (= event :app.events/editor-save-success)
                                 (do (resolve)
                                     (js/console.log (str "RESOLVE:" args) )
                                     (rf/remove-post-event-callback cb))
                                 
                                 (= event :app.events/editor-save-failure)
                                 (do (reject "POST failed")
                                     (js/console.log (str "REJECT:" args))
                                     (rf/remove-post-event-callback cb))))]
                   (rf/add-post-event-callback cb)
                   (js/console.log "saving")
                   (rf/dispatch [:editor/save data])))))
The :editor/save event handler uses re-frame.http-fx to do a POST and triggers save-success / save-failure depending on the result. This works but it seems wrong to hook into re-frame's event system like that. Is there a better way ?

p-himik09:05:10

I would definitely try to wrap that component in a plain Reagent component, and then use that wrapped component in re-frame.

p-himik09:05:34

Also you could pass resolve and reject to the :editor/save event. It wouldn't be great, but still better than fiddling with add-post-event-callback.

sylvain09:05:56

yeah I thought of that but I don't like passing JS functions in a dispatch vector

p-himik09:05:40

To my taste, it's much better than post event callbacks. Apart from being quite ugly (adding a callback that removes itself), it's also prone to concurrency issues when you dispatch more than one :editor/save. It may be fine in this case, but the pattern itself is not great.

sylvain09:05:23

I actually simplified my problem a bit. I'm already doing this inside a the :component-did-mount function of a (reagent/create-class call. But I get what you mean by needing to wrap it inside a plain reagent component (i.e no re-frame involved). I think it could work.

sylvain09:05:14

thanks

👍 4