As I’ve been hearing more about React, Ive been beginning to question my own usage of Blaze.
So there are two ways I can render data into my templates, each which represents a pole of reactivity granularity:
Far Right
Very fine grained reactivity; lots of observers, no unnecessary template re-renders.
<template name="post">
{{postTitle}}
{{postScore}}
{{postType}}
{{postUser}}
{{postHappy}}
{{postSad}}
{{postHungry}}
{{postHasManyManyFields}}
...
</template>
//the helpers look like this
//each helper is rerun only if the relevant data changes
postScore = function(){
var id = nonreactiveGetId();
Posts.findOne(id, {fields:{score:1}})
}
As the app becomes bigger, we start to see a lot of helpers and each one has its own Posts.findOne...
scoped to its own field. That’s a lot of observers.
Far Left
Reactivity at a coarse level; only one observer, tons of re-rendering stuff that doesn’t need to be rendered.
<template name="post">
{{#with post}}
{{postTitle}}
{{postScore}}
{{postType}}
{{postUser}}
{{postHappy}}
{{postSad}}
{{postHungry}}
{{postHasManyManyFields}}
...
{{/with}}
</template>
//the #with helper looks like this:
post = function(){
var id = nonreactiveGetId()
return Posts.findOne(id)
}
//the other look like this, with the appropriate changes made to each
//all helpers are rerun when ANY field changes.
postScore = function(){
return this.score
}
Middle
For large apps, there could be 20 or 30 spots that have dynamic data. The middle approach would be to establish logical groupings of reactivity based on related fields. Instead of 20-30 observers, we’d have maybe 3-5 (depends on the data, ofc)
Are observers so cheap compared to re-rendering that we should almost always go for the fine grained reactivity, Far Right approach?
If you’ve faced this with Blaze, did you notice any big setbacks with having arbitrarily fine reactivity? At what point did it become better to deal with the unnecessary re-renderings of Blaze, than to have N live queries open?
As always, thanks for any help, correction, clarification, or comment!