Skip to content

Instantly share code, notes, and snippets.

@jdubie
Last active August 29, 2015 14:27
Show Gist options
  • Save jdubie/17e396c65e389548cc1f to your computer and use it in GitHub Desktop.
Save jdubie/17e396c65e389548cc1f to your computer and use it in GitHub Desktop.
My first macro! Syntactic sugar for stateful reagent components.
(ns example.core
(:require-macros [example.macro :refer [defc]])
(:require [reagent.core :as reagent :refer [atom]]))
;; without macro
(defn component
"example stateful component"
[a b c d]
(let [state (atom nil)]
;; if you forget to return a function this will not live update
;; make sure too keep these parameters in sync with above!
(fn [a b c d]
[:div "hello world")))
;; using macro - less boilerplate -> less mistakes!
(defc component
"example stateful component"
[a b c d]
[state (atom nil)]
[:div "hello world"])
(ns example.macro)
(defmacro defc
"syntactic sugar for declaring stateful components for reagent"
[component-name params let-block body]
`(def ~component-name (fn ~params
(let ~let-block
(fn ~params ~body)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment