Fork me on GitHub
#re-frame
<
2020-04-29
>
Patrick Truong17:04:58

Hello everyone, I’m working through a basic re-frame example following the intro guide. I have a very simple `dispatch-sync [:initialize]` call and a simple event handler:

(rf/reg-event-db
 :initialize
 (fn [_ _]
   {:message "I'm initialized!"}))
However, I’m getting this error in my console:
core.cljs:3912 re-frame: no :event handler registered for: :initialize
eval @ core.cljs:3912
eval @ core.cljs:3907
eval @ core.cljs:3901
eval @ core.cljs:3895
eval @ core.cljs:3889
eval @ core.cljs:3924
eval @ loggers.cljc:40
eval @ registrar.cljc:31
re_frame$events$handle @ events.cljc:57
re_frame$router$dispatch_sync @ router.cljc:265
alpha_journal$core$main @ core.cljs:24
eval @ shadow.module.main.append.js:4
goog.globalEval @ main.js:836
env.evalLoad @ main.js:2224
(anonymous) @ main.js:2429
This is my core.cljs (entry file):
(ns alpha-journal.core
  (:require
   [reagent.core :as r]
   [reagent.dom :as dom]
   [re-frame.core :as rf]
   [alpha-journal.views :refer [app]]))

(defn render
  []
  (dom/render [app]
              (js/document.getElementById "app")))

(defn ^:dev/after-load clear-cache-and-render!
  []
  ;; The `:dev/after-load` metadata causes this function to be called
  ;; after shadow-cljs hot-reloads code. We force a UI update by clearing
  ;; the Reframe subscription cache.
  (rf/clear-subscription-cache!)
  (render))

(defn main
  []
  (rf/dispatch-sync [:initialize])
  (render))
This is my events.cljs:
(ns alpha-journal.events
  (:require
   [re-frame.core :as rf]))

(rf/reg-event-db
 :initialize
 (fn [_ _]
   {:message "I'm initialized!"}))
Currently I’m not subscribing anywhere as I just wanted to inspect the app db in REPL. Does this possibly cause the error? Any thoughts are much appreciated 🙂 thanks

ingesol17:04:14

You are not requiring your events.cljs from your main file, that’s the problem

Patrick Truong17:04:09

Whoops, just realized I needed to require my events namespace for it to load. @ingesol thank you for the catch! Can someone possibly explain why this is necessary (not sure why requiring events ns causes it to all sync)? Coming from a Redux background there is an obvious place where Redux is loaded (`<Provider store={store}/>), is there a visible place where re-frame is wrapping or initializing all the event handlers, app-db, etc.? Thanks!

ingesol17:04:39

re-frame has registries for storing state like events and subscriptions, and they are hidden from you. You only set and get them through keywords. The reason you have to require is because of the way clojurescript compilation works. Namespaces that are not used will not be included/parsed in your program

ingesol17:04:25

So when your events ns is not required by anyone, it will not be run during runtime, and then no event registration happening

Patrick Truong17:04:42

@ingesol thanks for the great explanation 🙂

👍 1
Patrick Truong22:04:41

Hello everyone, I’m currently setting up re-frame-10x with a basic project that is using shadow-cljs only. I got the go-ahead from Thomas Heller that my config looks good. I’m now sure why, but my app-db only shows up correctly in the dashboard when I hot reload my app (change a view file for example). Anyone who got a shadow-cljs app working with re-frame-10x, I would love to hear if you had to do anything special. Here’s my shadow-cljs.edn:

{:deps   true
 :builds {:app {:target     :browser

                :output-dir "public/out"

                :asset-path "/out"

                :modules    {:main {:init-fn alpha-journal.core/main}}

                :compiler-options {:closure-warnings {:global-this :off}
                                   :closure-defines {"goog.DEBUG" true
                                                     "re_frame.trace.trace_enabled_QMARK_" true
                                                     "_frame.tracing.trace_enabled_QMARK_" true}
                                   :optimizations :whitespace}

                :devtools   {:http-root "public"
                             :http-port 8020
                             :after-load alpha-journal.core/render
                             :preloads  [devtools.preload
                                         day8.re-frame-10x.preload]}}}}
My deps.edn:
{:paths ["src"]
 :deps  {thheller/shadow-cljs        {:mvn/version "2.8.94"}
         binaryage/devtools          {:mvn/version "1.0.0"}
         cider/cider-nrepl           {:mvn/version "0.25.0-alpha1"}
         reagent                     {:mvn/version "0.10.0"}
         re-frame                    {:mvn/version "0.12.0"}
         day8.re-frame/tracing       {:mvn/version "0.5.3"}
         day8.re-frame/tracing-stubs {:mvn/version "0.5.3"}
         day8.re-frame/re-frame-10x  {:mvn/version "0.6.2"}}}
and my basic app file, core.cljs:
(ns alpha-journal.core
  (:require
   [reagent.dom :as dom]
   [re-frame.core :as rf]
   [alpha-journal.events]
   [alpha-journal.views :refer [app]]))

(defn render
  []
  (dom/render [app]
              (js/document.getElementById "app")))

(defn main
  []
  (rf/dispatch-sync [:initialize])
  (render))
Looking to hear possible issues with my setup. Thanks for the help!