Skip to content

Instantly share code, notes, and snippets.

@andykitchen
Created July 8, 2014 07:52
Show Gist options
  • Select an option

  • Save andykitchen/7b2f31834f00d554f798 to your computer and use it in GitHub Desktop.

Select an option

Save andykitchen/7b2f31834f00d554f798 to your computer and use it in GitHub Desktop.
(ns cljs-spike.core)
(defn vector->react [v]
(let [[el attrs body] v
react-body (react body)]
(if (keyword? el)
(make-react-dom-component el attrs react-body)
(el attrs react-body))))
(defn make-react-dom-component [el attrs react-body]
(let [component (aget js/React.DOM (name el))]
(component (clj->js attrs) react-body)))
(defn react [el]
(cond
(string? el) el
(seq? el) (clj->js (map react el))
(vector? el) (vector->react el)))
(defn create-class [opts]
(let [react-class (js/React.createClass (clj->js opts))]
(fn [props]
(when (not (map? props)) (throw (js/Error. "component props should be clj map")))
(let [js-props (clj->js props)]
(aset js-props "_clj" props)
(react-class js-props)))))
(defn props [this]
(aget this "props" "_clj"))
(def videos
[{:name "one"}
{:name "two"}
{:name "three"}])
(defn render-item [name]
[:li {:className "video-item"} name])
(def VideoListItem
(create-class
{:displayName "VideoListItem"
:render #(this-as this (-> this props :name render-item react))}))
(defn render-items [items]
[:ul {:className "video-list"}
(for [item items]
[VideoListItem item])])
(def VideoList
(create-class
{:displayName "VideoList"
:render #(this-as this (-> this props :videos render-items react))}))
(js/React.renderComponent
(VideoList {:videos videos})
(js/document.getElementById "my-app"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment