Why subscription is stopping in React component?

Hi, when I call Meteor.subscribe() in componentDidMount(), it gets stopped automatically and I don’t know why since I don’t call the stop() method in the code.

Also this only happens when I reload the page, because if I go to another page and come back to the page of the subscription, it loads normally and don’t stops…

Do you have any idea ?

Here is an extract of the component :

componentDidMount() {
        const params = this.props.params || {};
        const self = this;

        if (params._id) {
            Shipowners.find({_id: params._id}).observe({
                added(doc) {
                    console.log(doc);
                    self.setState({shipowner: doc});
                }
            });
            Meteor.subscribe("shipowner", params._id);
        }
    }

I wouldn’t bother looking for solution. Use a container to fetch data and make sure your component does not depend on Meteor: https://guide.meteor.com/react.html#using-createContainer

2 Likes

Okay, I finally found where was the problem after a few days of investigation…
I have this code used by the route’s onEnter method that redirects to the login page if the user is not logged in :


const requireAuth = function (nextState, replace, callback) {
    Tracker.autorun((c) => {
        if (!Meteor.loggingIn()) {
            if (!Meteor.userId()) {
                const location = nextState.location.pathname;
                const url = location || this.path;
                replace("/login?redirect=" + encodeURIComponent(url));
            }
            // c.stop(); // fixme: this causes subscriptions to stop
            callback();
        }
    });
};

As it’s commented in the code, the c.stop() was causing the subscription to stop immediately.
I don’t master the Meteor’s Tracker API, but I guess the subscription relies on the Tracker, that’s why it was stopped.