There are three forms:
1. You only need a render function (no setup or other lifecycle functions)
2. You need setup and render (in re-frame's case, this is to subscribe to
state)
3. You need other React lifecycle functions

To illustrate why both form 1 and 2 are useful, consider code like in this
rather contrived example:

(defn form-1 [foo]
    [:div foo])

(defn form-2 []
    (let [x (subscribe [:x])]
      (fn []
        [:div [form-1 @x]])))


Each time you call subscribe, it creates new reagent reactions, sets up
listeners etc, so you wouldn't want to do that on every render - best to do
it once when the component is first mounted.

As for multiple subscribes, yes, that works. See the re-frame sample
project:
https://github.com/Day8/re-frame/blob/master/examples/simple/src/simpleexample/core.cljs#L73

On Sun, 22 Mar 2015 at 18:34 Sven Richter <[email protected]> wrote:

> Ok, I read that again and do understand what you mean. I will convert them
> to the form-2 pattern.
> I just wonder, why did it work the way I implemented it? What is the
> rationale behind the forms? Can I find something about that to read?
>
> Another question that came just up for me, is, can I have multiple
> subscriptions in the let binding like this:
>
> (defn foo []
>   (let [files (subscribe [:files])
>         groups (subscribe [:groups])]
> ...
>
>
> Best Regards,
> Sven
> Am Sonntag, 22. März 2015 18:26:16 UTC+1 schrieb Daniel Kersten:
> > I believe the key thing with subscribe is that it happens outside of the
> render function.
> >
> > When you do this:
> >
> >
> > (defn foo []
> >     (let [s (subscribe ...)]
> >       (fn []
> >         [:div ...])))
> >
> >
> > The call to subscribe happens once, when the component is created. The
> inner function is called each render, but it closes over the subscription
> 's' - it does not call subscribe again.
> >
> >
> > On the other hand, in this code:
> >
> >
> > (defn bar []
> >     (let [s (subscribe ...)]
> >       [:div ...]))
> >
> >
> > There is no anonymous function being returned, so its called every
> render and subscribe is called every render.
> >
> >
> > More details here: https://github.com/Day8/re-frame/wiki/Creating-
> Reagent-Components#the-three-ways and especially here
> https://github.com/Day8/re-frame/wiki/Creating-Reagent-
> Components#form-2--a-function-returning-a-function
> >
> >
> >
> > On Sun, 22 Mar 2015 at 17:06 Sven Richter <[email protected]> wrote:
> > Hi,
> >
> >
> >
> > thank you both for your suggestions.
> >
> >
> >
> > I guess I will separate by functionality and then by handlers / subs /
> ....
> >
> > I think for larger applications handlers and subs become harder to track
> down when you put them all into one file.
> >
> >
> >
> > > BTW, there is definitively some view code in that repo which worries
> me. You absolutely shouldn't be embedding calls to "subscribe" in the
> middle of view render functions:
> >
> > >    - https://github.com/sveri/siwf/blob/b5290c59c050119f3cf03512208542
> 7dadfba654/src/cljs/de/sveri/siwf/files/upload.cljs#L60
> >
> > >     - https://github.com/sveri/siwf/blob/
> b5290c59c050119f3cf035122085427dadfba654/src/cljs/de/sveri/
> siwf/files/uploded_files.cljs#L36
> >
> > >
> >
> > > I believe those lines would be odd reagent, much less re-frame.
> >
> >
> >
> > Hm, I never thought about that. What exactly would be better, putting
> them at the top of the function like so:
> >
> >
> >
> > (defn foo []
> >
> >   (let [files (subscribe ....
> >
> >
> >
> > Or pass them into the function from the very top like this:
> >
> >
> >
> > (defn foo [files]...
> >
> >
> >
> > (defn top []
> >
> >   (let [files (subscribe .... (foo files)...
> >
> >
> >
> >
> >
> > Best Regards,
> >
> > Sven
> >
> >
> >
> >
> >
> > Am Sonntag, 22. März 2015 10:19:55 UTC+1 schrieb Mike Thompson:
> >
> > > On Sunday, March 22, 2015 at 12:37:20 AM UTC+11, Sven Richter wrote:
> >
> > > > Hi,
> >
> > > >
> >
> > > > I was integrating re-frame in the last days into an application that
> I am working on. Now, after finishing one "page" I get the feeling that my
> structure is messy and after a few days of a break I have a hard time to
> mentally follow the dataflow in my application, related to the code.
> >
> > > >
> >
> > > > This are the three files I am referring to:
> >
> > > > https://github.com/sveri/siwf/blob/b5290c59c050119f3cf03512208542
> 7dadfba654/src/cljs/de/sveri/siwf/files/files.cljs
> >
> > > > https://github.com/sveri/siwf/blob/b5290c59c050119f3cf03512208542
> 7dadfba654/src/cljs/de/sveri/siwf/files/upload.cljs
> >
> > > > https://github.com/sveri/siwf/blob/b5290c59c050119f3cf03512208542
> 7dadfba654/src/cljs/de/sveri/siwf/files/uploded_files.cljs
> >
> > > >
> >
> > > >
> >
> > > > Any ideas how to do that "better"? Especially the fact that I cannot
> really follow mentally makes it hard for me to code in this pattern. Harder
> than I expected tbh as the derived values thing really makes sense in
> general.
> >
> > > >
> >
> > > > Anything else worth pointing out in that code?
> >
> > >
> >
> > > Hi Sven,
> >
> > >
> >
> > > I follow the layout in the example todomvc (in the repo) which
> involves handlers.cljs, subs.cljs, db.cljs, views.cljs and I find
> navigation of the resulting source fairly easy.  Its always seems pretty
> clear where to look for something.
> >
> > >
> >
> > > Mike Haney's code might also provide some guidance:
> >
> > > https://github.com/mdhaney/homesale-clj/tree/master/src/homesale
> >
> > >
> >
> > > Your example has the complication of using resumablejs, but I can see
> you are updating it in event handlers, which seems the right approach to me
> (although I know little of resumablejs).
> >
> > >
> >
> > > BTW, there is definitively some view code in that repo which worries
> me. You absolutely shouldn't be embedding calls to "subscribe" in the
> middle of view render functions:
> >
> > >    - https://github.com/sveri/siwf/blob/b5290c59c050119f3cf03512208542
> 7dadfba654/src/cljs/de/sveri/siwf/files/upload.cljs#L60
> >
> > >     - https://github.com/sveri/siwf/blob/
> b5290c59c050119f3cf035122085427dadfba654/src/cljs/de/sveri/
> siwf/files/uploded_files.cljs#L36
> >
> > >
> >
> > > I believe those lines would be odd reagent, much less re-frame.
> >
> > >
> >
> > > --
> >
> > > Mike
> >
> >
> >
> > --
> >
> > Note that posts from new members are moderated - please be patient with
> your first post.
> >
> > ---
> >
> > You received this message because you are subscribed to the Google
> Groups "ClojureScript" group.
> >
> > To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected].
> >
> > To post to this group, send email to [email protected].
> >
> > Visit this group at http://groups.google.com/group/clojurescript.
>
> --
> Note that posts from new members are moderated - please be patient with
> your first post.
> ---
> You received this message because you are subscribed to the Google Groups
> "ClojureScript" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/clojurescript.
>

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to