createContainer vs. tracker autorun

Hi. I am using React with Meteor 1.3 and the alt.js implementation of flux.
I am trying to figure out the best way of subscribing to publications. So far I have been doing it this way:

Tracker.autorun(() => {
  const coachHandle = Meteor.subscribe('activities.show', id);
  if (coachHandle.ready()) {
    const activity = Activity.findOne({ _id: id });
      this.fetchActivitySuccess(activity);
   }
 });

An action runs the above code when the component mounts.
The reason I’m doing this, is to have the action handle the subscribtion.

But the official meteor guide suggests using something like this (in the component):

export default createContainer(({params}) => {
  console.log(params.activityId);
  Meteor.subscribe('activities.show', params.activityId);
  return {
    activity: Activity.findOne({ _id: params.activityId })
  };
}, ActivityShowContainer);

Is there any downside to my original way of doing things?

One downside would be that you bind the component to a certain action, muddling the waters between the router, the data, and the view. I like to mount self-contained react components, like your second example, which makes them reusable in other places.

1 Like

Thank you jorgeer.

I just prefer to have all data fetching handled by flux rather than react.

I’m using both techniques on my site.

The Tracker.autorun gets used when dealing with global data and events not specific to one particular container or set of containers. For example, the User() state - quite a lot of containers I’m using have to look at the user state somehow whether to determine what data to display, what functions to allow or simply whether to display at all. I could use createContainer but that would create a lot of repetition.

Container-specific stuff gets indeed pulled in by createContainer.

3 Likes