[英]When do I have access to props in the react class?
I am passing a meteor collection as a prop in one of my components and trying to figure it out when do I actually receive props? 我正在将流星集合作为道具传递到我的一个组件中,并试图弄清楚何时真正收到道具?
I tried accessing (for eg this.props.userData
) the props in getInitialState
is says undefined, then I tried accessing it in componentDidMount it says undefined, but in render method it works fine. 我尝试访问(例如
this.props.userData
) getInitialState
的props表示未定义,然后尝试在componentDidMount中访问其表示未定义的道具,但是在render方法中它可以正常工作。
Which method before or after render
can tell me that I have access to props? render
之前或之后的哪种方法可以告诉我可以使用道具? I want to initialize the state with the values props will have. 我想使用props将具有的值来初始化状态。
for example in the code below I get an error saying that userData is undefined. 例如,在下面的代码中,我收到一条错误消息,提示未定义userData。
getInitialState(){
return{
firstName : this.props.userData.firstName
}
}
Edit So I am doing something like this, I am using props just to initialize the state variable. 编辑所以我正在做这样的事情,我只是在使用props来初始化状态变量。
export default React.createClass({
getInitialState(){
return {
email : this.props.user.email
}
},
onFormSubmit(event){
event.preventDefault();
},
onTextFieldChange(event){
switch (event.target.name) {
case "email":
this.setState({
email:event.target.value
});
break;
}
},
render() {
console.log(this.props.user.email);
return (
<div className="panel panel-default">
<div className="panel-heading">
<h3 className="panel-title text-center"><strong>Sign in with Existing Account</strong></h3>
</div>
<form className="form-horizontal" id="frmSignIn" role="form" onSubmit={this.onFormSubmit}>
<div className="modal-body">
<div className="form-group">
<label className="col-xs-4 control-label">Email</label>
<div className="col-xs-8">
<input type="email" id="email" name="email" className="form-control"
value={this.state.email}
onChange={this.onTextFieldChange}
placeholder="example@domain.com"
required/>
</div>
</div>
<div className="form-group">
<label className="col-xs-4 control-label">Password</label>
<div className="col-xs-8">
<input type="password" id="password" name="password" className="form-control"
placeholder="Password"
required/>
<label id="signin-error" className="error"> </label>
</div>
</div>
</div>
<div className="panel-footer">
<label className="col-xs-4 control-label"></label>
<div className="col-xs-8">
<input type="submit" className="btn btn-primary pull-right" value="SignIn"/>
</div>
</div>
</form>
</div>
);
}
});
The component receives the initial properties immediately on instantiation. 该组件在实例化后立即接收初始属性。 Actually it is the first argument of the constructor:
实际上,这是构造函数的第一个参数:
class MyComp extends React.Component {
constructor (props) {
super(props)
// do something with props
}
componentWillReceiveProps (nextProps) {
// properties changed
}
}
In your case my best guess is that the parent component does not yet have your property ready. 在您的情况下,我最好的猜测是父组件尚未准备好您的属性。 You can track the passed property by observing them in the constructor and in a
componentWillReceiveProps(nextProps)
function. 您可以通过在构造函数和
componentWillReceiveProps(nextProps)
函数中观察传递的属性来跟踪它们。
Meteor synchronises user data just like any other collection with the server. 流星与其他任何集合一样,与服务器同步用户数据。 It means that initially
Meteor.user()
will return undefined, and your component doesn't receive the prop. 这意味着最初
Meteor.user()
将返回未定义,并且您的组件不接收该道具。 Later when the user document is updated, your component's props are updated, too. 稍后,当用户文档更新时,组件的属性也将更新。 You can catch this event by implementing
function componentWillReceiveProps(newProps) {...}
. 您可以通过实现
function componentWillReceiveProps(newProps) {...}
来捕获此事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.