将ReactJs Props分配给FlowRouter

时间:2016-01-04 12:17:25

标签: meteor flow-router

如何将道具从FlowRouter传递到我的反应组件。那可能吗?文档很棒。

我正在做这样的事情:

FlowRouter.route('/dashboard', {
  name: 'dashboard',
  action(){
    var x = Projects.find().fetch(); // this not working
    console.log(x); // x is []. Why?
    ReactLayout.render(App, {
      nav: <Nav />,
    content: <Profile data={x}/>
    });
  }
});

在我的应用中,我想说this.props.data,但数组是空的。我必须将逻辑放入react组件中。这是正确的方法吗?我希望不会。

1 个答案:

答案 0 :(得分:0)

我认为您需要订阅...请参阅此处的文档https://github.com/kadirahq/flow-router#subscription-management

FlowRouter.route('/dashboard', {
  name: 'dashboard',
  subscriptions(){
      this.register('myProjects', Meteor.subscribe('projects'));
  },
  action(){
    ReactLayout.render(App, {
      nav: <Nav />,
      content: <Profile data={myProjects}/>
    });
  }
});

但经过进一步审核后,他们实际上建议您在React组件中获取流星数据...请参阅此处的文档

https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management/with-react

  Profile = React.createClass({
     mixins: [ReactMeteorData],
     getMeteorData() {
       var data = {};
       var handle = Meteor.subscribe('projects');
       if(handle.ready()) {
         data.projects = Projects.find({});
       }
       return data;
     },    
  });

示例项目:https://github.com/aaronksaunders/meteor-react-demo2