(def a 1)
(def add-1 [x] (+ x 1))
(println (add-1 a))
http://learnxinyminutes.com/docs/clojure
A Demo.
brew install lumo
this
需要额外处理this.state
不够灵活, this.setState
过程式编程一切皆是表达式
(if (> x 10) "big" "small")
非常时候构造 DSL.
Clojure 语言非常稳定, 不接受社区的 PR. 只能通过类库进行扩展.
this
几乎没有 this
. 完全通过函数来构造, 而不是 class
.
基于不可变数居, 严格限制可变数据的使用
(def a {:a 1, :b 2})
(assoc a :c 3)
(def b [1 2])
(conj b 3)
(def c '(6 5 4))
(cons 7 c)
(def d #{:a :b :c})
(conj d :d)
this.state
用 atom
重新抽象 state:
(def click-count (r/atom 0))
(defn counting-component []
[:div
"The atom " [:code "click-count"] " has value: "
@click-count ". "
[:input {:type "button" :value "Click me!"
:on-click #(swap! click-count inc)}]])
Atom, 内置数据类型, Observable, 一个引用:
(def state-x (atom 0))
(add-watch state-x :logger (fn []
(println "Changed!")))
(reset! state-x 1) ; prints "Changed!"
(println @state-x) ; dereference to get value
http://reagent-project.github.io/
(defn simple-component []
[:div
[:p "I am a component!"]
[:p.someclass
"I have " [:strong "bold"]
[:span {:style {:color "red"}} " and red "] "text."]])
(defn render-simple []
(r/render-component [simple-component]
(.-body js/document)))
(defn atom-input [value]
[:input {:type "text"
:value @value
:on-change #(reset! value (-> % .-target .-value))}])
(defn shared-state []
(let [val (r/atom "foo")] ; <---- atom
(fn []
[:div
[:p "The value is now: " @val]
[:p "Change it here: " [atom-input val]]])))
(defn hello-component [name]
[:p "Hello, " name "!"])
(defn say-hello []
[hello-component "world"])
Some demos...
Thx
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |