2017-06-02 62 views
-1

我正在尝试创建一个容器,该容器从名为ProfileCandidate的集合中加载用户详细信息。我使用propTypes,我不确定我在做什么错误。我相信这是一个加载顺序错误,因为我的propType加载了一个空对象。我知道容器正在运行,因为它正在订阅我的profileCandidate.private请求。PropType不填充

路径:components/ContactDetails.js

class ContactDetails extends React.Component { 
    constructor(props) { 
    super(props); 
    var ProfileCandidate = this.props.profileCandidate; 
    console.log("ProfileCandidate: ", this.props.profileCandidate); 
    }; 
} 

ContactDetails.propTypes = { 
    profileCandidate: React.PropTypes.object.isRequired, 
}; 

export default createContainer(() => { 
    Meteor.subscribe('profileCandidate.private'); 
    var test = ProfileCandidate.findOne({userId: Meteor.userId()}); 
    console.log("test: ", test); 
    return { 
    profileCandidate: ProfileCandidate.findOne({userId: Meteor.userId()}) || {}, 
    }; 
}, ContactDetails) 
+0

你是否试图渲染视图?预计组件构建时会得到空对象,因为发布没有时间向客户端发送数据。不要期望构造函数中的'props'包含只在稍后重新运行容器后才可用于'render'的数据。 – MasterAM

回答

1

createContainer负载异步数据,所以profileCandidate的初始状态将是undefined。您可以在createContainer回调中看到您的数据,因为您在由MeteorDataManager in react-meteor-data line 107创建的执行上下文中,但在此之前ContactDetails使用空的道具呈现。

你应该基于只有当它的加载this.props.profileCandidate做逻辑,所以没有在costructorrendercomponentDidUpdate方法。

请更改propTypes,以便不再需要该属性,因为它将异步加载。请注意,从React v15.5开始已弃用React.PropTypes。请使用prop-types library instead