PropType没有填充

时间:2017-06-02 07:19:59

标签: reactjs meteor

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

路径: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)

1 个答案:

答案 0 :(得分:1)

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

您应该仅在this.props.profileCandidate加载时才基于costructor执行逻辑,因此不应在render中,而应在componentDidUpdatepropTypes方法中。

请更改React.PropTypes,以便不再需要该属性,因为它将异步加载。请注意,自React v15.5起,datausage = r"e:\temp\111.txt" strStr = ["192.168.42.12", "192.168.42.2"] with open(datausage) as f: lines = f.readlines() for ii in strStr: sumResult = 0 for line in lines: if ii in line: result = line ip = line[5:-50] result_ip = ip.replace(" ", "") usage = line[-8:] d = usage.replace('KB', '') usage = d.replace('B', '') usage = usage.replace('\n', '') sumResult += float(usage) print(result_ip + '\t\t\t' + str(sumResult)) 已弃用。请使用prop-types library instead