Fork me on GitHub

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?


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


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
                                 (do (resolve)
                                     (js/console.log (str "RESOLVE:" args) )
                                     (rf/remove-post-event-callback cb))
                                 (= event
                                 (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 ?


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


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.


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


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.


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.



👍 4