import React, { Component } from "react"; import { Link } from
"react-router-dom";
const emailRegx = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
export default class ChefInfo extends Component { constructor(props) { super(props); this.state = { eInput: "",
small: "" };
-
----------
---------
}
handleChange = e => {
this.setState({
eInput: e.target.value
});
};
emailTest = () => {
if (emailRegx.test(this.state.eInput) === false) {
this.setState({
small: "your email is inccorect"
});
} else {
this.setState({
small: ""
});
}
};
render() {
return (
<div className="big-parent">
<form>
<div className="input">
<label>
<strong>E-mail</strong>
</label>
<input
type="email"
className="input-filed"
onChange={() => { //here the problem
this.handleChange();
this.emailTest();
}}
/>
<small className="small">{this.state.small}</small>
</div>
</form>
<a href="#" className="btn btn-dark button">
<strong>READY</strong>
</a>
</div>
);
} }
答案 0 :(得分:1)
您的handlechange使用事件对象参数。
所以您应该传递事件对象。
onChange={(e) => { //here the problem
this.handleChange(e);
this.emailTest();
}}
但是在这种情况下,您不需要使用两个函数。
足够了。
handleChange = (e) => {
this.setState({
eInput : e.target.value,
small : emailRegx.test(e.target.value) ? '' : "your email is incorrect"
})
};
答案 1 :(得分:1)
您可以重构代码,使其看起来像这样。这样可使您的渲染/模板代码看起来更干净。
handleEvent(event) {
this.handleChange(event);
this.emailTest();
}
.
.
.
<input
type="email"
className="input-filed"
onChange={this.handleEvent}
/>