【发布时间】:2020-08-26 03:51:14
【问题描述】:
就我的代码而言,我认为我做的事情是正确的。在这一刻,我无法在输入中写入。有人知道会发生什么吗?下面附上我的代码:
const Login: SFC<LoginProps> = ({ history }) => {
const alertContext = useContext(AlertContext);
const { alert, showAlert } = alertContext;
const authContext = useContext(AuthContext);
const { message, auth, logIn } = authContext;
useEffect(() => {
if (auth) {
history.push('/techs');
}
if (message) {
showAlert(message.msg, message.category);
}
}, [message, auth, history]);
const [user, saveUser] = useState({
email: '',
password: ''
});
const { email, password } = user;
const onChange = (e: any) => {
saveUser({
...user,
[e.target.name]: e.target.value
})
}
const onSubmit = (e: any) => {
e.preventDefault();
if (email.trim() === '' || password.trim() === '') {
showAlert('All fields are required', 'alert-error');
}
logIn({ email, password });
}
return (
<>
...
<form onSubmit={onSubmit}>
<input type="text" ... value={email} onChange={onChange} />
<input type="password" ... value={password} onChange={onChange} />
<input type="submit" className="fadeIn third" value="Log In" />
</form>
...
</>
);
}
export default Login;
这就是组件的样子:
【问题讨论】:
-
你的输入中有名字吗?
标签: reactjs typescript forms input