This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-11-01
Channels
- # announcements (7)
- # babashka (41)
- # beginners (117)
- # cider (3)
- # clj-kondo (145)
- # cljdoc (25)
- # cljs-dev (19)
- # clojure (197)
- # clojure-dev (14)
- # clojure-europe (4)
- # clojure-italy (3)
- # clojure-nl (2)
- # clojure-spec (11)
- # clojure-uk (21)
- # clojuredesign-podcast (5)
- # clojurescript (29)
- # code-reviews (4)
- # cursive (87)
- # data-science (11)
- # datomic (29)
- # duct (2)
- # emacs (10)
- # graalvm (1)
- # lumo (13)
- # malli (2)
- # nrepl (5)
- # off-topic (25)
- # onyx (1)
- # pathom (6)
- # reagent (20)
- # reitit (4)
- # rewrite-clj (7)
- # shadow-cljs (114)
- # spacemacs (16)
@franquito You would write that component like this:
(defn main []
(let [inner-ref (atom nil)]
(fn []
^{:ref #(reset! inner-ref %)} [inner])))
There’s a guide to refs here: https://github.com/reagent-project/reagent/blob/master/doc/FAQ/UsingRefs.mdthat’s weird, React itself should take handle of assigning refs
I mean yes ref can be a function, but it can be an object as well where ref target is assigned under current
field.
Right, from the documentation I understood that for the ref to be an object you also need to use React.forwardRef
. But I wanted to understand what actually happens in this particular case of ^{:ref ref} [component]
. I've seen adding :key
as metadata, but never :ref
. About your comment @josh604 I've tried running
(defn inner []
[:div "Hello world!"])
(defn main []
(let [inner-ref (r/atom nil)]
(fn []
^{:ref #(js/console.log "Hi?")} [inner])))
But the Hi?
is never logged.Judging by the source code, I'm fairly certain the Reagent respects only :key
in the metadata.
ha! ok, sorry for confusion
btw since Reagent passes all args as actual arguments you don’t need forwardRef
because in JS React ref prop is treated specially
yeah, it seems obvious now that you can’t get a ref on a component, only on a DOM element 🙂
hm, I think it should be possible to refer to an instance of a component via refs
according to the React docs, only on class-based components https://reactjs.org/docs/refs-and-the-dom.html
but again, might be not needed in Reagent, since components are functions, not classes with public APIs
on the other hand internally Reagent components are created as React classes 🙂
passing ref
is confusing in React because they specifically dissoc it from props for class components.
but reagent doesn’t pass in arguments as regular props, it all gets bundled into an argv
prop
so if you give a reagent component a prop named ref
it will work as you would expect
It seems that you can still make something interesting with metadata - Reagent passes it directly to createReactClass
.
But it's the metadata for a function itself - not its usage, like in [f props]
. And the resulting React class becomes cached, where the function itself is the key. I.e. all usages of Using f
must use the same metadata for the results to be predictable.(with-meta f)
yields a different object, so the same f
can be reused with different metadata.