【发布时间】:2020-06-15 00:57:21
【问题描述】:
我正在尝试为应用程序实现一个简单的登录表单,但由于某种原因无法让我的三元运算符工作。我只是使用 json 文件导入登录凭据,并使用提交处理程序将用户输入与使用三元的数据进行比较。提交时,没有任何反应。请参阅下面的代码:
import React from 'react'
import customData from '../db.json'
import DocumentPage from './DocumentPage'
class MemberPortal extends React.Component {
state = {
username: "",
password: "",
}
handleUserChange = (event) => {
this.setState({username: event.target.value});
}
handlePwChange = (event) => {
this.setState({password: event.target.value});
}
handleSubmit = (event) => {
event.preventDefault();
return this.state.password == customData.password ? <DocumentPage /> : alert("incorrect login information")
}
render(){
return(
<div id ="test">
<form onSubmit = {()=>this.handleSubmit}>
<label for="username">User Name:</label>
<div className="ui input">
<input type="text" id="username" value = {this.state.username} onChange = {this.handleUserChange} placeholder = "Username"></input>
</div>
<label for="password">Password:</label>
<div className="ui input">
<input type="text" id="password" value = {this.state.password} onChange = {this.handlePwChange} placeholder="Password"></input>
</div>
<input className="ui blue button" type="submit" value="Submit" onClick = {this.handleSubmit}></input>
</form>
</div>
)
}
}
export default MemberPortal;
【问题讨论】:
-
您需要使用
this.props.history.push("/DocumentPage")重定向到另一个组件。在codesandbox.io/s/routing-7brm6 此处查看带有您的代码的沙箱......而不是customData.password,检查是针对" "(空密码)暂时可以改回customData.password..如果您在密码中输入内容并单击提交,则会发出警报.. -
如果从
return this.state.password == customData.password ? <DocumentPage /> : alert("incorrect login information")中删除return会发生什么? -
@DVN-Anakin 我做了,不幸的是没有工作。