Last active
August 29, 2015 14:27
-
-
Save jdubie/17e396c65e389548cc1f to your computer and use it in GitHub Desktop.
My first macro! Syntactic sugar for stateful reagent components.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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