day8 / re-com

A ClojureScript library of reusable components for Reagent
https://re-com.day8.com.au
MIT License
797 stars 146 forks source link

Since components have callbacks as arguments, do they always rerender? #146

Closed UrKr closed 7 years ago

UrKr commented 7 years ago

Since reagent using identical? to see if it has to rerender a component, does the fact that recom components take callbacks as arguments mean that this optimization can never work, even if all the other props are identical?

If this is true, I suppose a solution is to pass in globally defined functions. That is a bit inconvenient however, because you can't close over local variables.

mike-thompson-day8 commented 7 years ago

If you create the callback in-line, then it will not test identical? to previously. But if you create a Form-2 reagent function, and create the callback in an outer function for use within the renderer, then it will test identical? each time.

pesterhazy commented 7 years ago

Note that if you re-create the same anon fn twice, not only will it be not identical? but also not =:

> (def x (atom nil))
> (defn foo [] (let [f (fn [] (println "foo"))] (prn (= @x f)) (reset! x f) nil))
> (foo)
false
> (foo)
false
pesterhazy commented 7 years ago

I've created a live demo of @UrKr's difficulty: https://gist.github.com/pesterhazy/36b56d1bd8b424abef81adbb5a392d93 (scroll down for the link)

mike-thompson-day8 commented 7 years ago

Yep, covered here: https://github.com/Day8/re-frame/blob/master/docs/Performance-Problems.md#4-callback-functions

@UrKr are we good to close?

UrKr commented 7 years ago

@mike-thompson-day8 Yes. Thank you both for the quick replies!