Search code examples
meteorreactjsonloadaccount

meteorjs reactjs action after authentification


I have the following react component that passed data to another component:

export default class App extends TrackerReact(Component){

 getUserFrameData(){
   return (FrameCollection.find().fetch());
 }
 render(){
   return(
     <div className="main-container">
      <Frames
        data={this.getUserFrameData()}
       />
</div>
);
}
}

Now I want my frames component to do an action when the component initialises.

export default class Frames extends Component{
   componentDidMount(){
      console.log(this.props.data);
   }

   render() {...}

  }

But on I only get empty data at on loadup. I think it's because I'm using subscriptions and a login system. So how can I tell my Frames component to wait until everything is "loaded up"?


Solution

  • Use the ready method of the subscription object.

    constructor() {
      super();
      this.state = {
        sub: Meteor.subscribe('myPublication')
      }
    }
    
    render() {
      if (!this.state.sub.ready()) return <p>Loading...</p>;
      return ...
    }
    

    http://docs.meteor.com/api/pubsub.html#Subscription-ready