2016-01-06 111 views
0

是否有另外一種寫作方式?順便說一句,這完美的作品,但我覺得它可以寫更好:使用反應和流星進行雙向數據綁定

Profile = React.createClass({ 
    mixins: [ReactMeteorData], 
    getMeteorData() { 
    return { 
     currentUser: Meteor.user(), 
    }; 
    }, 
    getInitialState(){ 
    // we add an if statement to prevent undefined errors 
    // could this be written elsewhere? 
    if (Meteor.user().profile) { 
     this.profile = Meteor.user().profile; 
    } else { 
     this.profile = ''; 
    }; 
    return { firstname: this.profile.firstname }; 
    }, 

    ... 
)}; 

回答

0

是的,你可以使用ES6類,這與流星(ECMAScript中包有你覆蓋)工作時推薦的方式。另外,您不需要使用getMeteorData()方法以外的Meteor.user()。

class Profile extends React.Component { 
    constructor(props){ super(props); } 

    mixins: [ReactMeteorData] 

    getMeteorData() { 
    return { 
     user: Meteor.user(), 
    }; 
    } 

    getInitialState(){  

    return { 
     firstname: this.data.user && this.data.user.profile.firstname 
    }   
    } 
}