While you could use a callback instead, eg:
setError(error => ({ ...error, empty: "no empty values" }));
I think setting the state once would make a bit more sense. If a test doesn't generate an error, set the associated property to the empty string:
setError({
length: email === "" || password === "" ? 'no lengthy values' : '',
empty: email.length < 3 ? 'no empty values' : ''
});
Even better, separate out the different states:
const [emptyError, setEmptyError] = useState('');
and then, eg, call setEmptyError
instead when you need to set the error for empty
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…