2016-01-04 22 views
0

如何將道具從FlowRouter傳遞給我的反應組件。那可能嗎?文檔非常棒。將ReactJs道具分配給FlowRouter

Im做這樣的事情:

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但數組是空的。我必須將邏輯放入反應組件。那是正確的方法嗎?我希望不是。

回答

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}/> 
    }); 
    } 
}); 

但經過進一步審查,但實際上,建議你得到的陣營組件流星數據......看到這裏的文檔

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

+0

謝謝,但我得到'ReferenceError:myProjects未定義'指向'data = {myProjects}' – Sylar

+0

是的。謝謝 – Sylar