createContainer and Publications/Subscriptions

Hello,

I’m developing a React + Meteor app, and I’m wondering how to achieve the following:

I have a route that accepts a parameter for example to show the single page of a document, so the route accepts an ID parameter.

Now, in my createContainer, how can I redirect my users to another page if the Id passed does not exist? I’m not sure if this is something that should be done in the container or in the component?

Any advice?

Kr,

Henry

Are you accepting that ID parameter via component’s props?

Yes, I’m accepting it as a parameter in the URL (’/document/:id’ , in the react router) and it is passed to the container.

You could, inside of your createContainer function, check to see if the result of your operation is null and if it is conditionally call browserHistory.push('/nextPage')

Can you give an example? I’m working with subscriptions inside my container.

What router are you using? React-Router or Flow Router?

I’m using react-router.

Great! You can try this:

export default createContainer(()=> {

 Meteor.subscribe('my_collection');
 const my_collection =  MyCollection.findOne({_id: this.props.params._id});  //Be sure to have imported your Collection above!

//Check to see if you got a record from the collection
  if(typeof my_collection == "undefined"){
      browserHistory.push('/'); //Import this from React-Router package
  }
    return {
      my_collection: my_collection // could just use single var here to let ES6 do it's thing.
    }

}, MyComponent);