我正在尝试创建一个容器,用于从名为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)
答案 0 :(得分:1)
createContainer
异步加载数据,因此profileCandidate的初始状态为undefined
。您可以在createContainer
回调中查看数据,因为您处于由MeteorDataManager
in react-meteor-data line 107创建的执行上下文中,但在此之前,ContactDetails
将使用空道具进行呈现。
您应该仅在this.props.profileCandidate
加载时才基于costructor
执行逻辑,因此不应在render
中,而应在componentDidUpdate
或propTypes
方法中。
请更改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。