This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-06-11
Channels
- # announcements (1)
- # architecture (23)
- # beginners (189)
- # boot (1)
- # calva (62)
- # clj-kondo (23)
- # cljs-dev (9)
- # clojure (336)
- # clojure-dev (11)
- # clojure-europe (2)
- # clojure-italy (17)
- # clojure-nl (25)
- # clojure-uk (53)
- # clojurescript (12)
- # core-async (29)
- # data-science (1)
- # emacs (6)
- # fulcro (23)
- # garden (3)
- # graphql (2)
- # jobs (1)
- # joker (7)
- # off-topic (17)
- # om (2)
- # qlkit (1)
- # reagent (15)
- # reitit (18)
- # rewrite-clj (7)
- # shadow-cljs (176)
- # sql (1)
- # test-check (4)
- # vim (32)
- # xtdb (30)
without ref
, how to find a dom inside the component? I want focus on a input tag when a button is clicked.
You could still do that with ref
s, but another option is to just use id
’s and document.getElementById
a common thing people do is this:
(defn my-form []
(let [input-ref (r/atom nil)
handle-click (fn [] (when @input-ref (.focus @input-ref)))]
(fn []
[:div
[:button {:on-click handle-click} "Focus"]
[:input {:ref (fn [ref] (when ref (reset! input-ref ref)))}]])))
which was an old API that they’ve deprecated. the ☝️ above is using callback refs, which are definitely supported
@lilactown why there is a (when ref
?
sometimes the callback passed into the ref
prop will be called with a nil
value, I’m not totally clear why
The docs call that behaviour out: https://reactjs.org/docs/refs-and-the-dom.html#caveats-with-callback-refs
(and indeed moving the (fn [ref..
to the outer let/mount-phase solved it for me in the past)