This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-01-15
Channels
- # announcements (5)
- # architecture (17)
- # aws (2)
- # bangalore-clj (1)
- # beginners (157)
- # boot (22)
- # boot-dev (2)
- # cider (64)
- # clara (2)
- # cljs-dev (3)
- # clojure (30)
- # clojure-art (2)
- # clojure-australia (1)
- # clojure-belgium (1)
- # clojure-denver (1)
- # clojure-dusseldorf (1)
- # clojure-europe (8)
- # clojure-finland (2)
- # clojure-italy (9)
- # clojure-nl (21)
- # clojure-spec (261)
- # clojure-switzerland (3)
- # clojure-uk (67)
- # clojurescript (57)
- # clojurewerkz (2)
- # cursive (3)
- # datomic (27)
- # emacs (12)
- # figwheel-main (2)
- # fulcro (48)
- # garden (67)
- # graphql (41)
- # jobs (8)
- # kaocha (8)
- # liberator (2)
- # lumo (1)
- # off-topic (19)
- # parinfer (9)
- # perun (4)
- # re-frame (50)
- # reagent (7)
- # remote-jobs (4)
- # ring-swagger (20)
- # rum (6)
- # shadow-cljs (170)
- # specter (3)
- # tools-deps (19)
- # vim (3)
Hi!
Is it possible to get the ref
of a reactify
-ed component? I'm mocking a JSX component from CLJS and I can't get a pointer to that element's DOM node, getting what seems to be a constructor object instead.
(defn sub [props]
[:p {:ref #(js/console.log "comp:" %)} "Hello" (:name props)])
(defn hello [name]
[:div
[:p {:ref #(js/console.log "direct:" %)} "Hello" (:name props)]
[sub {:ref #(js/console.log "parent:" %) :name name}]
[:> (r/reactify-component sub) {:ref #(js/console.log "react:" %) :name name}]])
;; Console output after rendering [hello "Reagent"]
direct: <p>…</p>
comp: <p>…</p>
comp: <p>…</p>
react: t {props: {…}, context: {…}, refs: {…}, updater: {…}, state: null, …}
@aisamu If the React component doesn't use ref forwarding or provide separate dom element ref property, no. https://reactjs.org/docs/forwarding-refs.html
Got it, thanks!
Would this still apply if reagent is generating the React component from plain hiccup? (with reactify-component
, as in the example)
Hmh, I'm not completely sure what is happening here, but:
1. ref printing "parent: " is not used because sub
component doesn't use ref
from props anywhere
2. :>
and reactify-component
together might generate additional adapter class which might explain why both refs are called
Got it, thanks!
Would this still apply if reagent is generating the React component from plain hiccup? (with reactify-component
, as in the example)
If you want to create "JSX" or native React component, it might be better to just create real React function directly instead using reactify-component
.
(defn sub [props]
(react/createElement "p" #js {:ref #(js/console.log "comp:" %)} "Hello" (.-name props)))
And I guess you could try forwardRef also:
(def sub (react/forwardRef (fn [props ref] (react/createElement "p" #js {:ref ref} "Hello" (.-name props)))))