Skip to content

Instantly share code, notes, and snippets.

@tonsky
Created October 2, 2013 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonsky/6791188 to your computer and use it in GitHub Desktop.
Save tonsky/6791188 to your computer and use it in GitHub Desktop.
(defn component [ref render-fn & {:keys [on-mount on-unmount]}]
(let [comp (React/createClass (clj->js
{ :render (fn [] (crate/html (render-fn @ref)))
:componentDidMount (or on-mount identity)
:componentWillUnmount (or on-unmount identity) }))]
(add-watch ref :react
(fn [_ _ old new]
(when (not= old new) (.forceUpdate comp))))
comp))
(def state (atom {:title "Vasya"}))
(component state (fn [val]
[:div.class (str "Hello" (:title val))]))
(swap! state assoc :title "Petya")
@piranha
Copy link

piranha commented Oct 2, 2013

Я только что подумал, что может лучше будет так:

(defn component [ref render-fn]
   (let [comp (React/createClass (clj->js {:render (fn [] (tmpl (this-as this (render-fn (.-props this))))}))]
      (add-watch ...)
      comp))

(let [state (atom {:count 0})]
   (component state (fn [props]
       [:div.class {:on-click #(swap! state update-in [:count] inc)} (:count @state)]))

Не очень хорошо, что state надо и создавать, и передавать, но зато он спрятан, и это может макро исправить.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment