Fork me on GitHub
#reagent
<
2017-01-18
>
rovanion07:01:56

@mccraigmccraig Thank you so much for that component, it worked excactly as indended once I figured out what the arguments were. Do you want to answer that SO post and let me give you some magical internet points or would you allow me to publish a simplified version of your code as the answer?

mccraigmccraig09:01:40

np @rovanion - all my projects are built on the open-source efforts of many others, which i am very grateful for - helping out a little is the least i can do in return

mf15:01:37

Hi hoping someone can help, I'm attempting to do something a little out of the ordinary. I want to create a component that renders a "hidden" anchor element. When the component loads I want to automatically trigger a click event on the components root DOM node. This is what I have so far:

(defn my-btn []
  (reagent/create-class
   {:component-did-mount
    #(js/click (reagent/dom-node %))

    :reagent-render
    (fn []
      [:a {:style {:display "none"}}])}))
Now this won't work because js/click is not a function. Can someone advise on how I can achieve this?

pesterhazy16:01:50

that's really a javascript question though, isn't it? How to programmatically generate click events?

pesterhazy16:01:40

oh. do you mean just .click instead of js/click?

mf16:01:02

@pesterhazy yes looks likes that's what I need

mf16:01:39

of course as click is a method on the element in js

mf16:01:47

hence .click

mf16:01:31

Do you know how I could get reference to child elements within the component? Assuming the anchor was a nested element?

pesterhazy16:01:53

reagent/dom-node is semi-depricated

mf16:01:52

@pesterhazy ok will take a look, thanks

pesterhazy16:01:07

you can attach refs to subnodes in your tree

mf16:01:44

@pesterhazy yeah was wondering about how to do React refs

mf16:01:52

in reagent

mf17:01:30

@pesterhazy this is what I went with that is alot nicer than having to use the React lifecycle stuff:

(defn my-btn []
  (fn []
    [:a {:style {:display "none"}
         :ref (fn [el] (when-not (nil? el)
                         (.click el)))}]))

pesterhazy17:01:53

Yup that works too

mf17:01:21

@pesterhazy thanks for pointing me in the right direction 🙂

pesterhazy17:01:07

:ref is like a mini did-mount method 🙂

mf17:01:16

yeah it's neat

mf17:01:18

just have to remember that it's called when the component is destroyed too, except in this case the el passed to the function is nil.

pesterhazy17:01:10

I usually use some->

mf17:01:37

as in :ref (fn [el] (some-> el (.click el)))?

mf17:01:57

yeah that's nicer

mf18:01:49

Hi hoping someone can help. I have a higher order function that may accept either a Reagent component or a standard function as an argument. I want to invoke the supplied function with [] if it's a component and () if it's a standard function.

;; higher order function
(defn hof [f]
  ;; need to determine if f is a component
  [f])

(defn my-component []
  [:div "I'm a compnent"])

(defn not-a-component []
  "I'm NOT a component")

;; I want `not-my-component to be invoked with ()'
(hof not-a-component)

;; I want `my-component to be invoked with []'
(hof my-component)
Is there any way I can determine if the supplied function is a component?

mf18:01:01

I appreciate either can be called with (). But I need the Reagent components to be re-rendered independently of their siblings (hence invoked with [])

emccue23:01:38

Put some meta data in components maybe?

emccue23:01:57

But I don't see your purpose

emccue23:01:37

In what place are you wanting both functions that don't return components as well as components