Skip to content

Instantly share code, notes, and snippets.

@prestancedesign
Forked from saskali/re-frame-click-counter
Last active December 8, 2020 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prestancedesign/22f1fc6b3a0c4b9aba70d497c450e945 to your computer and use it in GitHub Desktop.
Save prestancedesign/22f1fc6b3a0c4b9aba70d497c450e945 to your computer and use it in GitHub Desktop.
Re-frame simple click counter
(ns simple.core
(:require [reagent.dom :as reagent]
[re-frame.core :as rf]))
;; -- Domino 1 - Event Dispatch -----------------------------------------------
(defn dispatch-click
[]
(rf/dispatch [:click-count])) ;; send event `:click-count`
;; -- Domino 2 - Event Handlers -----------------------------------------------
(rf/reg-event-db
:initialize ;; id for initialize event
(fn [_ _] ;; arguments: [db event]
{:counter 0})) ;; set start value to 0
(rf/reg-event-db
:click-count
(fn [db [_ _]]
(update db :counter inc))) ;; increase counter
;; -- Domino 4 - Query -------------------------------------------------------
(rf/reg-sub
:count ;; query id (used later in subscribe)
(fn [db _]
(:counter db))) ;; the function which will compute the query
;; -- Domino 5 - View Functions ----------------------------------------------
(defn view
[]
[:div
"The value of " [:code "app-db"] " currently is: "
@(rf/subscribe [:count]) " "
[:input {:type "button" :value "+"
:on-click dispatch-click}]])
;; -- Entry Point -------------------------------------------------------------
(defn ^:export run
[]
(rf/dispatch-sync [:initialize]) ;; puts a value into application state
(dom/render [view] ;; mount the application's ui into '<div id="app" />'
(js/document.getElementById "app")))
(run)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment