【发布时间】:2020-05-07 04:48:07
【问题描述】:
我在我的反应应用程序中使用材料 ui TextField。我需要知道如何对下面带有电子邮件和密码的代码使用错误和辅助文本验证? 请仔细阅读下面给出的代码,并为我提供一个合适的解决方案,我可以解决
import React, { Component } from 'react';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import AppBar from 'material-ui/AppBar';
import RaisedButton from 'material-ui/RaisedButton';
import TextField from 'material-ui/TextField';
import swal from 'sweetalert';
class Login extends Component {
constructor(props){
super(props);
this.state={
email: '',
password: ''
}
}
async handleClick() {
const { email, password } = this.state;
let options = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
}
let response = await fetch('/login', options);
response = await response.json();
if (!response) {
swal({
title: "Wrong Entry",
text: "Check your email and password",
icon: "warning",
button: "OK",
});
return;
}
if(response === 'user'){
await auth.userLogin();
this.props.history.push('/app/my');
return;
}
options = {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
}
let res = await fetch('/team-check', options);
res = await res.json();
await auth.login();
if (!res) {
this.props.history.push('/choose-teams')
} else {
this.props.history.push('/dashboard',{state: { detail: response }})
}
}
render() {
return (
<div>
<MuiThemeProvider>
<div>
<AppBar
title="User-Login"
/>
<TextField
hintText="Enter your Email"
floatingLabelText="email"
onChange = {(event,newValue) => this.setState({email:newValue})}
/>
<br/>
<TextField
type="password"
hintText="Enter your Password"
floatingLabelText="Password"
onChange = {(event,newValue) => this.setState({password:newValue})}
/>
<br/>
<RaisedButton label="Submit" primary={true} style={style} onClick={(event) => this.handleClick(event)}/>
</div>
<Link to="/forgot-password">Forgot-Password</Link>
</MuiThemeProvider>
</div>
);
}
}
const style = {
margin: 15,
};
export default Login;
我已经尝试了许多堆栈溢出的答案,但现在都已经过时了。请仔细阅读我的代码,我们将不胜感激
【问题讨论】:
-
为什么你在同一个函数中定义了两次选项?
标签: reactjs material-ui