开发者问题收集

无法使用 ReactJS 在输入字段中输入文本值

2019-10-23
69

我目前正在尝试使用 ReactJS 更改输入字段的值,但遇到了一些问题。我无法在输入字段中输入值。

我在其他几个问题中读到,我只需要将输入字段的 更改为状态元素,但我猜我仍然做错了什么。

我已删除 handleLogin 函数以节省更多空间,我只是认为它无关紧要,因为它已经在运行。

父组件:

 class LoginPage extends Component {
   constructor(props) {
     super(props);
     this.state = {
       isInvalidForm: null,
       isFirstLogin: false,
       user: null,
       email: '',
       password: '',
       newPassword: '',
       userAttr: null
     }
     this.handleLogin = this.handleLogin.bind(this);
     this.changePassword = this.changePassword.bind(this);
     this.handleChange = this.handleChange.bind(this);
   }
    changePassword = (event) => {
     event.preventDefault();
     const cognitoUser = this.state.user;
     const userAttr = this.state.userAttr;
     const newPassword = this.state.newPassword;
     cognitoUser.completeNewPasswordChallenge(newPassword, userAttr, {
     onSuccess: (result) => {
     console.log("NEW PASSWORD COMPLETED: ");
     console.log(result);
    },
    onFailure: (err) => {
     console.log(err);
    }
  });
}

handleChange = event => {
 event.preventDefault();
 const { name, value } = event.target;
 this.setState({ [name]: value });
};



     render() {
    return (
      <div>
        {this.state.isFirstLogin ? (
          <NewPassswordForm changePassword={this.changePassword} handleChange={this.handleChange} newPassword={this.state.newPassword} />
        ) : (
            <LoginForm handleLogin={this.handleLogin} handleChange={this.handleChange} email={this.state.email} password={this.state.password} />
          )}
      </div>
    );
  }

}

我只在我的 NewPasswordForm 组件中遇到此问题:

class NewPasswordForm extends Component {
    render() {
        return (
            <div>
                <h3> Confirm new Password</h3>
                <form onSubmit={this.props.changePassword}>
                    <div className="form-group">
                        <label htmlFor="exampleInputPassword2">Password</label>
                        <input
                            type="password"
                            name="password2"
                            value={this.props.newPassword}
                            onChange={this.props.handleChange}
                            className="form-control"
                            id="exampleInputPassword2"
                            placeholder="New Password"
                        />
                    </div>
                    <button type="submit" className="btn btn-primary">
                        Submit
                    </button>
                </form>
            </div>
        )
    }
}


export default NewPasswordForm;
2个回答

namevalue 不匹配。将 name="password2" 更改为 name="newPassword"

marzelin
2019-10-23
class LoginPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isInvalidForm: null,
      isFirstLogin: false,
      user: null,
      email: '',
      password: '',
      newPassword: '',
      userAttr: null
    }

  }
   changePassword = (event) => {
    event.preventDefault();
    const cognitoUser = this.state.user;
    const userAttr = this.state.userAttr;
    const newPassword = this.state.newPassword;
    cognitoUser.completeNewPasswordChallenge(newPassword, userAttr, {
    onSuccess: (result) => {
    console.log("NEW PASSWORD COMPLETED: ");
    console.log(result);
   },
   onFailure: (err) => {
    console.log(err);
   }
 });
}

handleChange = evt => {
const value = evt.target.value;
  this.setState({
    [evt.target.name]: value
  });
};



    render() {
   return (
     <div>

         <NewPasswordForm 
         changePassword={this.changePassword}
          handleChange={this.handleChange} 
         newPassword={this.state.newPassword}
         password={this.state.password} />

     </div>
   );
 }

}

修改后

class NewPasswordForm extends React.Component {
  render() {
      return (
          <div>
              <h3> Confirm new Password</h3>
              <form onSubmit={this.props.changePassword}>
                  <div className="form-group">
                      <label htmlFor="exampleInputPassword2">Password</label>
                      <input
                          type="password" 
                          name="password"
                          value={this.props.password}
                          onChange={this.props.handleChange}
                          className="form-control"
                          id="exampleInputPassword1"
                          placeholder="Password"
                      />
                      <input
                          type="password" 
                          name="newPassword"
                          value={this.props.newPassword}
                          onChange={this.props.handleChange}
                          className="form-control"
                          id="exampleInputPassword2"
                          placeholder="New Password"
                      />
                  </div>
                  <button type="submit" className="btn btn-primary">
                      Submit
                  </button>
              </form>
          </div>
      )
  }
}
akhtarvahid
2019-10-23