When using Reagent, how do I use React's refs
?
Credit: this entry is entirely based on Paulus Esterhazy's Reagent Mysteries series
We'll start with a code fragment, because it is worth a 1000 words:
(defn video-ui []
(let [!video (clojure.core/atom nil)] ;; stores the
(fn [{:keys [src]}]
[:div
[:div
[:video {:src src
:style {:width 400}
:ref (fn [el]
(reset! !video el))}]]
[:div
[:button {:on-click (fn []
(when-let [video @!video] ;; not nil?
(if (.-paused video)
(.play video)
(.pause video))))}
"Toogle"]]])))
Notes:
fn
. The same technique would work with a Form-3 component.!video
. In this example, the state we capture is a reference to a video HTML element.!video
is a clojure.core/atom
and not a reaagent.core/atom
. We don't use a normal atom because refs never change during the lifecycle of a component and if we used a reagent atom, it would cause an unnecessary re-render when the ref callback mutates the atom.:video
component there's a :ref
callback function which establishes the state in !video
. You can attach a ref callback to any of the Hiccup elements.@!video
is used with the :button's
:on-click
to manipulate the video
DOM methods.Up: FAQ Index
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close