Fork me on GitHub
#reagent
<
2017-02-14
>
borkdude15:02:32

Is this an anti-pattern in Reagent, or just fine?

(defn foo [arg1] [:div (str arg1)])
(defn bar [arg1 arg2] [:div (str arg1 arg2)])
(defn parent [child]
  [:div
   child])

[:div
   [parent [foo "foo"]]
   [parent [bar "1" "2"]]]

borkdude15:02:49

Or this:

(defn precalculated []
  (let [state (r/atom 10)]
    (fn []
      [:div
       (into [:div] (range @state))
       [:button {:on-click #(swap! state inc)}
        "Click me button”]])))
I always wonder if Reagent behaves correctly when you dynamically create these vectors or calculate them before passing them as props.

pesterhazy16:02:38

why an anti-pattern? can you explain?

borkdude16:02:12

No, I just don’t know

pesterhazy16:02:54

afaik that's exactly how reagent is intended to be used 🙂

borkdude16:02:51

I saw some strange things with re-rendering in the past. Maybe it was due to the use of (foo …) vs [foo …]

borkdude17:02:04

What if you use [(decide-which-component-to-use …) “foo”]?

pesterhazy17:02:53

still works fine

pesterhazy17:02:23

there are a couple of things to look out for when it comes to re-rendering

pesterhazy17:02:49

the most important being lazy sequences (avoid them! 👍 for your into above)

pesterhazy17:02:10

another is multimethods (avoid)

nrako18:02:16

@pesterhazy can you elaborate a bit more / have any resources on the multimethod code smell? Didn't see it on the blog... 🙂

pesterhazy18:02:56

multimethods are aweseome generally

pesterhazy18:02:49

there's a known issue with reagent however where if you use a multimethod as a component, the component doesn't rerender properly

pesterhazy18:02:01

don't have a pointer to a github issue unfortunately

pesterhazy18:02:20

honestly at this point it's more hearsay, but @pupeno ran into it recently as well I believe

pesterhazy18:02:48

it's a shame because multimethods would make for great (polymorphic) components

nrako18:02:36

Thanks for the heads up. It's directly related to some work that I am doing, so I will keep an eye out. Appreciate it...

pesterhazy18:02:01

if you can identify the issue and create a github issue, that'd be great

borkdude18:02:12

@pesterhazy I’ve got my React Virtualized Infinite Loader working now btw. Hooked it up with Re-frame. Thanks for the help.

Pablo Fernandez19:02:28

I remember it working fine about six months ago. I picked up the same projects and now it's not working. Really puzzling and annoying.