I'm trying to study react hooks
. this is a sign-up form that works well when using the classical class component with internal state and controlled forms. but when I try to use react hooks
like this and type on the input it just will not display what I'm typing.
I have logged the event and have realized that the problem could be that the target value is null. Can somebody explain to me why this could be the case?
const SignUp = props => {
const [displayName, setDisplayName] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const handleChange = e => {
console.log(e);
const { name, value } = e.target;
switch (name) {
case name === "displayName":
setDisplayName(value);
case name === "email":
setEmail(value);
case name === "password":
setPassword(value);
case name === "confirmPassword":
setConfirmPassword(value);
}
};
const handleSubmit = async e => {
console.log(e);
e.preventDefault();
if (password !== confirmPassword) {
alert("passwords do not match");
return;
}
const { signUpStart } = props;
signUpStart({ email, password, displayName });
};
return (
<div className="sign-up-section">
<form onSubmit={handleSubmit}>
<FormInput
type="text"
name="displayName"
handleChange={handleChange}
value={displayName}
label="display name"
/>
<FormInput
type="email"
required
name="email"
value={email}
handleChange={handleChange}
label="email"
/>
<FormInput
type="password"
name="password"
handleChange={handleChange}
value={password}
label="password"
/>
<FormInput
type="psssword"
name="confirmPassword"
handleChange={handleChange}
value={confirmPassword}
label="comfirmPassword"
/>
<Button type="submit" name="password" label="SIGN" />
</form>
</div>
);
};
const FormInput =({label,handleChange, ...otherProps})=>{
return <div className='group'>
<input {...otherProps} onChange={handleChange} className='form-input'/>
{
label?(<label className={`${otherProps.value.length? 'shrink':''} form-input-label` }>{label}</label>):null
}
</div>
}