I have been working of a basic from using React and I need your help to improve this part of my code, especially validate
and onSubmit
.
state = {
email: "",
emailError: "",
};
change = e => {
this.setState({
[e.target.name]: e.target.value
});
};
validate = () => {
let isError = false;
const errors = {
emailError: "",
};
if (this.state.email.indexOf("@") === -1) {
isError = true;
errors.emailError = "Requires valid email";
}
this.setState({
...this.state,
...errors
});
return isError;
};
onSubmit = e => {
e.preventDefault();
const err = this.validate();
if (!err) {
this.props.onSubmit(this.state);
this.setState({
email: "",
emailError: "",
});
}
};
render() {
return (
<form>
<TextField
name="email"
hintText="Email"
floatingLabelText="Email"
value={this.state.email}
onChange={e => this.change(e)}
errorText={this.state.emailError}
floatingLabelFixed
/>
<br />
<RaisedButton label="Submit" onClick={e => this.onSubmit(e)} primary />
</form>
);