How to subscribe with parameters in a reactive container?

I am relatively new to React and looking to see how easy it would be to migrate to it from Blaze. I have picked a component to rewrite in React which contains a tree structure of nodes, but struggling with subscriptions.

I have created the following container

import { createContainer } from 'meteor/react-meteor-data';

export default createContainer (() => {
    return {
        openNodes: Session.get("openNodes"),
        selectedNodeId: Session.get("selectedNodeId"),
        getChildNodes: (node) => {
            Meteor.subscribe("nodesByParent", node['_id']});
            return Nodes.find({parentId: node['_id']}).fetch();
        }

    };
}, NodeWrapper);

The properties from Session work fine e.g. openNodes, selectredNodeId. However, when I call the getChildNodes method I see something strange. Using the ‘mongol’ tool (which allows me to see what is in minimongo), I can see the documents from the subscription appear and then instantly disappear.

Can someone please point me to the correct way to call a subscription with a parameter. Note: The getChildNodes method will not always be called - it only needs to be called when a user expands a node in the tree.

Very interesting! So here’s a bit of background on how subscriptions work - they actually automatically unsubscribe when the autorun they are inside ends. So for example, if you did:

Tracker.autorun((computation) => {
  Meteor.subscribe('data');
  computation.stop();
});

You would see the exact same situation, where the data appears and then immediately disappears.

I expect this is something similar, depending on when you are calling getChildNodes?

Are you calling it in an onClick handler?

No, the onClick of a node updates the ‘openNodes’ session variable to set which are open. The render of the node component looks to see if it is in openNodes and if it is then getChildNodes is called.

Why not just use the open nodes session variable to subscribe?

Thank you - I will give that a try.