This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-08-30
Channels
- # admin-announcements (1)
- # aws (32)
- # bangalore-clj (1)
- # beginners (2)
- # boot (137)
- # cider (2)
- # clara (1)
- # cljs-dev (39)
- # cljsrn (20)
- # clojure (268)
- # clojure-berlin (20)
- # clojure-canada (37)
- # clojure-dev (8)
- # clojure-gamedev (6)
- # clojure-norway (2)
- # clojure-russia (55)
- # clojure-spec (130)
- # clojure-uk (39)
- # clojurebridge (1)
- # clojurescript (102)
- # cursive (20)
- # datomic (231)
- # editors (5)
- # editors-rus (8)
- # events (5)
- # funcool (12)
- # hoplon (31)
- # instaparse (57)
- # jobs (9)
- # lein-figwheel (4)
- # off-topic (2)
- # om (8)
- # om-next (30)
- # onyx (241)
- # planck (6)
- # protorepl (4)
- # re-frame (115)
- # reagent (7)
- # rum (9)
- # schema (1)
- # test-check (9)
- # untangled (24)
- # yada (20)
How can I trigger a click event on an element inside a Reagent component? I'd usually use React Refs but these are not available in Reagent
@martinklepsch Reagent does allow you to specify a :ref
which can be a function or a string (here is a function example: http://stackoverflow.com/questions/39122243/reagent-component-that-orders-spans-by-their-computed-sizes)
For the alternative file selection button I prefer to just wrap the input in a label (clicking the label opens the file dialog):
[:label
[:input
{:type "file"
:accept "image/*"
:style {:display "none"}
:on-change
(fn image-selected [e]
(let [r (js/FileReader.)]
(set! (.-onload r)
(fn [e]
(reset! img (.. e -target -result))))
(.readAsDataURL r (aget (.. e -target -files) 0))))}]
[:span.mdl-button.mdl-button--icon
{:title "Background"}
[:i.material-icons "image"]]]
But based upon the stack overflow you linked earlier, it is impossible to trigger a click except from an event generated by the user. Assuming you are on that path, then calling .click
on the DOM element should be fine, the DOM element could be captured from either component-did-mount
or the ref
... I think you know all this so can you explain in more detail what the issue is?@timothypratley so I eventually ended up just using a random UUID as :id
+ goog.dom/getElement
which is a reasonable solution I guess. I know that I can pass refs to nodes but what I fail to understand is how I can retrieve them. Using a function as value (which I didn't know about) it seems I could stash the element in an atom so I can retrieve it later but that seems a good bit more complex than how you'd use refs usually
@martinklepsch, ref callbacks are great and pretty simple. For some background see https://github.com/timarney/react-faq#refs
@pesterhazy thanks! Didn't know about callback refs so that didn't come to mind but seems like a solid approach
For now I'm ok with just giving it a random ID, not perfect but certainly good enough ¯\(ツ)/¯