简体   繁体   English

未在表单上设置React js状态

[英]React js State not being set on forms

First am pretty new to react. 首先是很新的反应。 Am trying to change state of form inputs as follows 我正在尝试更改表单输入的状态,如下所示

constructor(props) {
    super(props);
    this.state = {
        pwd:'',
        email:'',
        value:''
    };
    this.handleEmailChange = this.handleEmailChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.handlePasswordChange = this.handlePasswordChange.bind(this);
}
handlePasswordChange(event){
    //this.setState({value: event.target.value});//Works
    this.setState({pwd: event.target.pwd});// Fails 
}

My problem is I am not able to set the state of the password field and instead am getting a warning saying Warning: A component is changing a controlled input of type password to be uncontrolled My render method is as bellow 我的问题是我无法设置密码字段的状态,而是收到一条警告,提示Warning: A component is changing a controlled input of type password to be uncontrolled我的渲染方法如下

 render(){
    return(
        <div
        className="container col-md-6 col-md-offset-3"
        >
      <Form horizontal onSubmit={this.handleSubmit}>
          <FormGroup
          controlId="formHorizontalEmail">
              <Col componentClass={ControlLabel} sm={2}>
                  Email
              </Col>
              <Col sm={10}>
                  <FormControl type="email" placeholder="Email"
                               value={this.state.email} onChange={this.handleEmailChange}/>
              </Col>
          </FormGroup>
          <FormGroup
              controlId="formHorizontalPassword"
              >
                  <Col componentClass={ControlLabel} sm={2}>
                      Password
                  </Col>
                  <Col sm={10}>
                      <FormControl type="password" placeholder="Password"
                                   value={this.state.pwd} onChange={this.handlePasswordChange}
                          //if I change to value={this.state.value} it works
                      />
                  </Col>
          </FormGroup>
          <FormGroup>
              <Col smOffset={2} sm={10}>
                  <Checkbox>Remember me</Checkbox>
              </Col>
          </FormGroup>
          <FormGroup>
              <Col smOffset={2} sm={10}>
                  <Button type="submit">
                      Sign in
                  </Button>
              </Col>
          </FormGroup>
      </Form>
        </div>
    );
}

Note: I have omitted some code that I think is not crucial. 注意:我已经省略了一些我认为不重要的代码。

Just change: 只是改变:

this.setState({pwd: event.target.pwd});// Fails 

for: 对于:

this.setState({pwd: event.target.value});

The event has an attribute target which has an attribute value with the value of the field at that particular moment. 该事件具有属性target ,该属性target的属性value与该特定时刻的字段值相同。 Hence you should assign that value to the pwd state attribute that you are defining :) 因此,您应该将该值分配给要定义的pwd state属性:)

Always remember that you are sending an object to the setState function. 始终记住,您正在将对象发送到setState函数。 The key of that object is the one that you are defining ( pwd ). 该对象的密钥是您要定义的密钥( pwd )。 The value is just the thing that you assign to that key, it could be a number, a string... or the value of an object's attribute (like in this case the string that contains event.target.value ). 该值只是您分配给该键的东西,它可以是数字,字符串...或对象属性的值(例如,在本例中为包含event.target.value的字符串)。

I'll show you a little snippet that I'm using in production: 我将向您展示我在生产中使用的一些代码段:

This is your state: 这是您的状态:

constructor(props) {
super(props);
this.state = {
    pwd:'',
    email:''
};

So, now you may want to build a form in order to change these values. 因此,现在您可能需要构建一个表单来更改这些值。 I'd recommend to do so: 我建议这样做:

<input name="pwd" onChange={(e) => this.handleChange(e)} ...>
<input name="email" onChange={(e) => this.handleChange(e)} ...>

So now you can do so: 所以现在您可以这样做:

handleChange(e){
 this.setState({[e.target.name]: e.target.value})
}

e.target.name takes the name of your input field (in that case: 'pwd' or 'email') and e.target.value takes its value. e.target.name采用输入字段的名称(在这种情况下为“ pwd”或“ email”),而e.target.value采用其值。

When you're writing more complex forms, this can help you a lot! 当您编写更复杂的表格时,这可以为您提供很多帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM