ClojureScript 編寫 React 組件:Reagent

y37f 10年前發布 | 23K 次閱讀 Reagent JavaScript開發

Reagent 是一個簡單的 ClojureScript 接口到 React. Reagent 提供一種使用 ClojureScript 高效編寫 React 組件的方法。

示例

Reagent uses Hiccup-like markup instead of React's sort-of html. It looks like this:

(defn some-component []
  [:div
   [:h3 "I am a component!"]
   [:p.someclass 
    "I have " [:strong "bold"]
    [:span {:style {:color "red"}} " and red"]
    " text."]])

You can use one component inside another:

(defn calling-component []
  [:div "Parent component"
   [some-component]])

And pass properties from one component to another:

(defn child [name]
  [:p "Hi, I am " name])

(defn childcaller []
  [child "Foo Bar"])

You mount the component into the DOM like this:

(defn child [name]
  [:p "Hi, I am " name])

(defn childcaller []
  [child "Foo Bar"])

assuming we have imported Reagent like this:

(defn mountit []
  (reagent/render-component [childcaller]
                            (.-body js/document)))

State is handled using Reagent's version ofatom, like this:

(def click-count (atom 0))

(defn state-ful-with-atom []
  [:div {:on-click #(swap! click-count inc)}
   "I have been clicked " @click-count " times."])

項目主頁:http://www.baiduhome.net/lib/view/home/1426388995271

 本文由用戶 y37f 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!