This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-11
Channels
- # admin-announcements (26)
- # aws (1)
- # beginners (356)
- # boot (28)
- # cider (20)
- # clara (12)
- # cljs-dev (78)
- # cljsrn (22)
- # clojure (333)
- # clojure-brasil (1)
- # clojure-dev (15)
- # clojure-miami (1)
- # clojure-nl (3)
- # clojure-russia (61)
- # clojurecup (3)
- # clojurescript (137)
- # clojurex (4)
- # core-async (4)
- # data-science (3)
- # datavis (2)
- # datomic (31)
- # editors (1)
- # emacs (9)
- # hoplon (3)
- # juxt (8)
- # ldnclj (47)
- # leiningen (4)
- # luminus (4)
- # off-topic (20)
- # om (332)
- # onyx (1)
- # parinfer (23)
- # portland-or (4)
- # proton (161)
- # reagent (46)
- # ring-swagger (11)
- # specter (7)
- # yada (2)
With only form-1 components without arguments, does it matter whether I use them with ()
or []
?
Because it will return a react component, whereas calling it with ()
will return a vector of hiccup
Ahh, because a react component will render what you'd expect to the DOM, but a vector of hiccup wont
If you don't have arguments and don't dereference any ratoms in a component it amounts to the same outcome I suppose.
But as soon as you have arguments or ratom dereferences it starts to behave differently.
@jaen Interesting. What if I subscribe and deref in the form-1 function? Does it make a difference then?
If you have a component called with []
and nested inside you have calls with ()
of subcomponents, it will appear to work
But what is happenijg is the top level os converted to a react component and the inner ones arent.
If you will call it with ()
then that hiccup gets put into the calling component and it will all re-render when the dereffed ratom changes.
So if I call g inside f, and g derefs, f will re-render everytime the atom changes, right?
It's explained quite nicely here - https://github.com/Day8/re-frame/wiki/Using-%5B%5D-instead-of-%28%29
(function ...)
is a normal Clojure function call that will be evaluated when it's called.
I read that. It doesn't say that [:div] is not turned into a react component, or did I miss that somewhere?
It also doesn't say the opposite, so it really doesn't say anything about that question, if I read the page correctly.
(maybe, but it answers the question about how this behaves wrt. rendering, as I described before)
In plain react, without JSX, you write HTML using React.createElement
(which creates components), so in the end yes, even [:div "h1"]
is a component.
But the difference is when you do (function ...)
you evaluate your component function right when it is encountered, in the normal strict evaluation Clojure fashion.
with [function ...]
this is evaluated lazily by reagent and it creates components when needed.
I didn't test, but I suspect if you tried to call a form-2 component with (function ...)
it would break, since it returns not hiccup, but a function.
So think about it as copy&pasting component's content vs. actually creating a reusable component.
Sometimes the first might be what you want (I did a simple form library for myself and doing that for fields worked okay), other times - not.
So, given the discussion above, what should I add to that doc about ()
and []
which would help to make things more clear?
[]
results in a distinct React Component with its own lifecycle. (Eg: it won't rerender unless props change).
()
is just a function call which returns something (hiccup in this case)