简体   繁体   English

我什么时候可以使用react类中的道具?

[英]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.userDatagetInitialState的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.

相关问题 如何从类外部的fineuploader const回调访问我的react组件props? - How do I access my react component props from a fineuploader const callback outside the class? 我如何查看 React 中哪些 props 发生了变化? - How do I see what props have changed in React? 如何访问 React props 中的嵌套对象属性 - How do I access nested object properties within React props 如何在类组件React之外访问props - How to access props outside of a class component React 在 React 组件中访问类实例道具? - Access Class Instance Props in React Component? 如果我可以直接访问和使用this.props和this.state,那为什么我必须在react native中通过构造函数中的super(props)调用它们? - If i can access and use the this.props & this.state directly then why i have to call them via the super(props) in constructor in react native? 当我尝试访问它时无法访问反应道具数据。 获取 TypeError 错误 - Can not access react props data when I try to access it. Getting TypeError error 我如何渲染作为带有附加道具的道具传入的反应组件? - How do i render a react component passed in as a props with additional props? 如何将道具传递到反应组件? - How do I pass props to a react component? 如何在 this.props.children 中访问 React 对象的类名 - How to access the class name of a React object in this.props.children
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM