I'm trying change error state after two password states become equal. But when I finish typing passwords onchange doesn't execute it requires to execute one more time.
*on using onkeyup events It works but i dont if its a good practice or not. I've also tried making it a timeout function but it didn't help either
Here are my functions.
const [password, setPassword] = useState('')
const [password2, setPassword2] = useState('')
const [passError, setPerror] = useState(false)
const handleChange = (event) => {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
console.log(name +" "+ value);
if(name == 'username') setUsername(value);
if(name == 'email') setEmail(value);
if(name == 'pass') setPassword(value);
if(name == 'pass2') setPassword2(value);
setTimeout(()=>checkPass(), 2000);
}
const checkPass = () => {
if (password != password2) setPerror(true);
else setPerror(false);
console.log(passError)
}
Here is my html.
<input type="password" className="form-control" name="pass" placeholder="Password" required=""
value={password} onChange={handleChange} onKeyUp={handleChange} style={{marginBottom: 0}} />
<input type="password" className="form-control" name="pass2" placeholder="Confirm Password" required=""
value={password2} onChange={handleChange} />
{ passError && <p className='alert-danger'>Password does not match!</p>}