Subscription not working in child component

Whenever I subscribed to publication in my app, the data returned correctly in parent component, but empty when I do subscription in the child component. Any clue about this?

This thread exactly has the problem that i facing now.
http://stackoverflow.com/questions/40112938/meteor-subscribe-works-only-on-top-level-react-component

Updates:

  • In child component, I try to put meteor.subscribe in the function right before “return Post.find().fetch()” and I able to see the data retrieved correctly. It just don’t work when i call like this in child component.
constructor(props) {
        super(props);
        
        this.state = {
            subscription: {
                Posts:Meteor.subscribe('Posts',props.val)
            },
        }
    }
  • I’m not sure what’s happening, but I finally got it to work like this :slight_smile:
constructor(props) {
    super(props);
    
    this.state = {
      subscription: {
        Post:() => Meteor.subscribe('Posts',props.val)    
      },
}

I dont know if you are using some library like tracker-react, but if you use the approach recommended in the meteor guide you should put all of your subscriptions inside container components so that their lifecycle is managed correctly.

1 Like

@sashko
Yes, I did use the library tracker-react in my app
And I do subscriptions in the component class constructor following the example of Tracker React. In this way, I can stop the subscription in componentWillUnmount().

I found some sources that people suggest putting the subscription in the render method like this thread.

My question is how to ensure the subscription stop when using this method.