reactjs - 无法在 react.js 中访问功能组件变量作为回报
问题描述
我想通过访问它来显示在 useState 中声明的错误变量,但它会抛出错误"TypeError: Cannot read property 'error' of undefined".
这是我到目前为止所尝试的。
Const Login = () => {
let history = useHistory();
const [form,setForm] = useState({
email:'',
password:'',
error:''
})
const handleSubmit = async(e)=>{
e.preventDefault();
let a = await login(form);
console.log(a);
if(typeof a !== "undefined") {
history.push('/Trial')
}
else {
this.state.error = "Invalid Email or Password";
}
console.log('Logged in')
console.log(e);
}
return (
<div>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<title>Sign In</title>
<link rel="stylesheet" href="login.css"></link>
</head>
<body>
<div className="container1 bg">
<div className="forms-container">
<div className="signin">
<form className="sign-in-form" onSubmit={handleSubmit}>
<h2 className="title">Sign In</h2>
<div className="input-field">
<i className="fas fa-envelope"></i>
<input type="email" placeholder="Email Address" onChange={(e) => setForm({...form, email: e.target.value})}/>
</div>
<div className="input-field">
<i className="fas fa-lock"></i>
<input type="password" placeholder="Password" onChange={(e) =>setForm({...form, password: e.target.value})}/>
</div>
<button class="btn1 solid">Login</button>
<p class="social-text" style={"color: red"}>{this.error}</p>
</form>
</div>
</div>
)
解决方案
推荐阅读
- oracle - Oracle 18 c trigger error sql ; 执行触发器时出错
- php - 在 Opencart 3.0 中检查结帐条件?
- python - 如何使用具有多个参数和多个返回字段的单个函数调用的输出将新列添加到新数据框中?
- c - 为什么这个 c 程序没有返回我所期望的?
- node.js - 部署到 Heroku 时 Webpack 别名不起作用
- css - Web devtools:继承的样式以删除线显示
- system-verilog-assertions - System Verilog 断言属性中的符号 '->' 和 '|->' 有什么区别
- python - 如何在一个范围内的while循环中总结整数
- php - 如何在php中使用require或include函数时调用函数内部的变量
- python - 如何使用 OpenMP 以并行模式在图形工具包中运行 SBM 算法?