2016-01-06 109 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 
    }   
    } 
}