【问题标题】:TypeError: Cannot read properties of undefined (reading 'state') reactjsTypeError:无法读取未定义的属性(读取“状态”)reactjs
【发布时间】:2021-11-18 23:28:50
【问题描述】:

我正在尝试在 reactjs 的 onClick 中控制台记录用户名和密码,但我收到了 TypeError。我在类的构造函数下定义了身份验证函数。下面是完整的代码。有人可以在这里指导我吗?

 authenticate(e){
        e.preventDefault();
        var details = {
            "email" : this.state.username,
            "password" : this.state.password
        }
        alert(details.username + details.password);


 <input type="email" onChange = {(event,newValue) => this.setState({username:newValue})} className="form-control" name="email" placeholder="Enter your Email" required="required" />

<input type="password" onChange = {(event,newValue) => this.setState({password:newValue})} className="form-control" name="password" placeholder="Enter your Password" required="required" />

<button className="btn w-100 mt-3 mt-sm-4" onClick={this.authenticate} type="submit">Sign In</button>

【问题讨论】:

    标签: javascript html css reactjs


    【解决方案1】:
    var details = {
                "email" : this.state.username,
                "password" : this.state.password
            }
            alert(details.username + details.password);
    

    在这里,您调用details.username,但详细信息将只有键emailpassword

    所以基本上,details.username 是未定义的,您无法读取该属性。

    试试改成

    var details = {
                    "email" : this.state.username,
                    "password" : this.state.password
                }
                alert(details.email + details.password);
    

    【讨论】:

      猜你喜欢
      • 2019-10-08
      • 2021-12-06
      • 2022-07-22
      • 2019-07-25
      • 2021-11-17
      • 2021-12-28
      • 2020-06-17
      相关资源
      最近更新 更多