If all you want is the hiccup interpreter, you may simple import hx.hiccup:
(require '[hx.hiccup :as hiccup])
(hiccup/parse [:div "foo"]) ;; => {$$typeof: Symbol(react.element), type: "div", key: null, ref: null, props: {…}, …}
hx.react contains a number of helpful functions and macros for doing React
development in ClojureScript.
An alias of hx.hiccup/parse, so that if you need to do some ad-hoc
transformation of hiccup one doesn't have to import hx.hiccup as well as
hx.react.
This macro is just like defn, but shallowly converts the props object passed
in to the component to a Clojure map and intelligently interprets the return
value of the body as hiccup. If the body doesn't return a vector, it simply
returns that value.
Takes a name, props bindings and a function body.
Example usage:
(require '[hx.react :as hx])
(require '[hx.hiccup])
(hx/defnc Greeting [{:keys [name] :as props}]
[:span {:style {:font-size "24px"}}
"Hello, " name "!"])
(react/render
(hx.hiccup/parse [Greeting {:name "Tara"}])
(. js/document getElementById "app"))
This macro creates a React component class. Is the CLJS equivalent of
class {name} extends React.Component { .... constructor is passed in this
and must return it. Additional methods and static properties can be passed in,
similar to defrecord / deftype. Methods are automatically bound to this.
Example usage:
(hx/defcomponent MyComponent
(constructor [this]
(set! (. this -state) #js {:name "Maria"})
this)
^:static
(greeting "Hello")
(update-name! [this e]
(. this setState #js {:name (.. e -target -value)}))
(render [this]
(let [state (. this -state)]
[:div
[:div (. my-component -greeting) ", " (. state -name)]
[:input {:value (. state -name)
:on-change (. this -update-name!)}]])))
An alias for react/createElement. It will marshall props from a map to JS and
interpret any hiccup children.
Creates a factory function from a component (e.g. a function, class, or string) that, when called, returns a React element.
Short-circuits the hiccup interpreter to return just the hiccup form returned by
body. Very useful for testing React components created using hx.hiccup.
Example:
(hx/defnc Welcome [{:keys [age]}]
(if (> age 17))
[:div "You're allowed!"]
[:div [:a {:href "http://disney.com"}] "Please go elsewhere"])
;; in test
(deftest welcome-allowed
(is (= (hx/shallow-render (Welcome {:keys age}))
[:div "You're allowed!"]))
(is (= (hx/shallow-render (Welcome {:age 17}))
[:div [:a {:href "http://disney.com"}] "Please go elsewhere"])))
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |