Consider this sample. Let's say we have this structure and we want to check some state value in the onChange
handler of an input. In this case I've used the input value to do the if
check, and if the condition doesn't pass, we set up an error. However, when I type in 10 characters, the input doesn't trigger the if block right away, but on the next typing. I know that state changes are asynchronous, which is why we'd mostly use useEffect
, but in this case, I cannot use it for an event handler.
So I am wondering what is the best way to handle this type of situations, when you need to check some state value inside an event handler and perform some actions based on that.
import { useState } from "react";
export default function App() {
const [value, setValue] = useState("");
const [error, setError] = useState(false);
function onSubmit(event) {
event.preventDefault();
}
function onChange(event) {
if (value.length < 10) {
setValue(event.target.value);
} else {
setError(true);
}
}
return (
<form onSubmit={onSubmit}>
<input type="text" value={value} onChange={onChange} />
{error && <h2>Error triggered</h2>}
</form>
);
}