Fork me on GitHub
#re-frame
<
2021-05-29
>
Ovidiu Stoica06:05:34

Does anyone have any idea why re-frame won’t recognise a handler i have? Handler :

(rf/reg-fx
  :ajax/get
  (fn [{:keys [url success-event error-event success-path]}]
    (GET url
         (cond-> {:headers {"Accept" "application/transit+json"}}
                 success-event (assoc :handler
                                      #(rf/dispatch
                                         (conj success-event
                                               (if success-path
                                                 (get-in % success-path)
                                                 %))))
                 error-event   (assoc :error-handler
                                      #(rf/dispatch
                                         (conj error-event %)))))))
event-fx that dispatches it:
(rf/reg-event-fx
  :messages/load
  (fn [{:keys [db]} _]
    {:ajax/get {:url           "/api/messages"
                :success-path  [:messages]
                :success-event [:messages/set]}}))
And i get this in the console:
re-frame: no handler registered for effect: {ns: "ajax", name: "get", fqn: "ajax/get", _hash: 1697424003, cljs$lang$protocol_mask$partition0$: 2153775105, …} . Ignoring.
I am kinda lost on this one

Ovidiu Stoica06:05:04

They are in separate namespaces but I don’t think that is the problem

lloydshark07:05:14

Is the namespace that defines the :ajax/get handler definitely loaded?

p-himik07:05:59

That, and also seems like you would benefit from installing https://github.com/binaryage/cljs-devtools

Ovidiu Stoica07:05:14

@U4T99SHSB I think you are correct. The namespace that has the ajax handler is not required in any other place. Would simply requiring it in the main ns solve this?

Ovidiu Stoica07:05:46

@U2FRKM4TW thank you. I am a beginner with clojurescript so any other good resources like that one are much apreciated 😄

p-himik07:05:25

Also https://github.com/binaryage/dirac then, but I'm not sure whether it works with shadow-cljs right now. And, well, shadow-cljs if you work with NPM packages a lot. And since you're using re-frame, https://github.com/Day8/re-frame-10x or https://github.com/flexsurfer/re-frisk

❤️ 3
Ovidiu Stoica07:05:41

Thank you, that fixed it 💪

p-himik07:05:57

There are many other tools that help during development of course.

Lukas11:05:04

Hey, I'm trying to get a component with react ref and re-frame sub working. My problem is, that when data gets dispatched a new "view" is created instead of updating the existing one. This probably ain't that hard, but I'm fairly new to reagent/re-frame. This is what I've got so far

(defn result-view [source]
  (let [!view (atom nil)]
    (fn [source]
      ;; update the ref
      (when-let [v @!view]
       (. v (dispatch (. (. v -state)
                         (update  #js {:changes {:from 0 :insert "0"}})))))
      [:div
       {:ref
        (fn [el]
          (reset!
           !view
           (EditorView.
            #js {:state
                 (.create
                  EditorState
                  #js {:doc (with-out-str (fipp source))
                       :extensions extensions-read-only})
                 :parent el})))}])))

(defn result-box []
  (let [source (rf/subscribe [:results])]
    (fn []
      (let [[_ form] @source]
       [:div {:class "code-box"}
        [result-view form]]))))
I'm really grateful for any pointer how to get this running 😊

p-himik11:05:06

subscribe returns a Reagent reaction, so this question isn't really re-frame-specific. If you don't get an answer here, I'd try asking in #reagent

Lukas11:05:34

okay thank you I give it a shot

p-himik11:05:37

The "if you don't get an answer here" was an important part. :) In general, you should wait for at least a few hours. People are in different time zones and have different schedules.

Lukas11:05:55

Thanks again, deleted the post on #reagent 🙂

👍 3
lilactown14:05:28

there's a couple issues with your code

lilactown14:05:55

I think the one that's biting you rn is that you're passing in a new function to the :ref prop every render, which is causing React to reinitialize the ref

lilactown14:05:36

if you move the ref fn that creates the EditorState outside of the inner render fn of your form-2 component, then it should stop recreating it every render

lilactown14:05:51

I'm guessing that calling .dispatch on the view during render with some dummy data is just a test you're doing, but I'm inferring that you want to render the current source passed to the results-view component in the editor

lilactown14:05:51

in that case, you'll want to use a form-3 component that has a component-did-update method which will dispatch the update to the view immediately after a successful render. this way, your render fn stays pure and you won't run into weird bugs later

Lukas14:05:42

Thank you a lot for this detailed explanation 😍

Lukas14:05:33

I will give it another shot with the stuff I just learned 🙏

Lukas15:05:02

@U4YGF4NGM awesome it seems to work 💪

(defn result-view2 [source]
  (let [!view (atom nil)
        !mount (fn [el]
                   (reset!
                    !view
                    (EditorView.
                     #js {:state
                          (.create
                           EditorState
                           #js {:doc (with-out-str (fipp '(load-db db-name "resources")))
                                :extensions extensions})
                          :parent el})))]
    (r/create-class
     {:display-name "result-view"
      :component-did-update
      (fn []
        (println "hallo?"))
      :reagent-render
      (fn [source]
        [:div {:ref !mount}])}))) 
👆 thanks again 😇