Search code examples
clojureclojurescriptre-frame

No view is being rendered in re-frame app


Here's a simple re-frame app that I tried to create based on the existing example project in re-frame's github repo. But it is only displaying things from the html file. Seems like no event is being dispatched. Can anyone point out what am I doing wrong? Thanks.

(ns simple.core
  (:require [reagent.core :as reagent]
            [re-frame.core :as rf]
            [clojure.string :as str]))

(rf/reg-event-db
  :rand
  (fn [db [_ _]]
    (assoc db :winner ( + 2 (rand-int 3)))))

(rf/reg-sub
  :winner
  (fn [db _]
    (:winner db)))

(def participants ["Alice" "Bob" "Ellie"])

(defn winners-name
  [idx]
  (get participants idx))

(defn show-winner 
  []
  [:h1
   (winners-name
    (@(rf/subscribe [:winner])))])

(defn ui
  []
  [:div
   [:h1 "Lottery"]
   [show-winner]])

(defn ^:export run
  []
  (rf/dispatch-sync [:rand])
  (reagent/render [ui]
                  (js/document.getElementById "app")))

Solution

  • The issue is caused because of a pair of extra parenthesis around the deref thing. So the function winners-name is treating it as a list instead of an integer.

    (winners-name
      (@(rf/subscribe [:winner]))