1

Which form is better?

(defn my-func [arg arg2]
 (fn [] [:div (str arg arg2)]))

Or this one?

(defn my-func []
 (fn [arg arg2] [:div (str arg arg2)]))

I'm writting from my phone so forgive me indentation...

akond
  • 15,865
  • 4
  • 35
  • 55

2 Answers2

1

There's other difference between these functions aside from arity.

The first function will close over arg, and arg2 on first render, and use these arguments for each subsequent rerender. E.g.

(def a 1)
(def b 2)
[my-func a b]

will always result in [:div '12'] whether or not a or b were changed

The second function will take arg, and arg2 on each render and will update the result when arguments change.

Look for Rookie mistake in reagent docs

jahson
  • 56
  • 1
  • 4
0

I believe your question should be which one is correct(The first one is correct). The second one has got an arity exception. Based on your use case, I would prefer to define and call the anonymous function so as to get the results. Consider this number one edit:

(defn my-func [arg arg2]
  ((fn [] [:div (str arg arg2)])))
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
JobSam
  • 287
  • 5
  • 11