I have component:
const FormComponent = () => {
const { register, handleSubmit, errors } = useForm();
const [val, setVal] = useState("");
const handleSubmitForm = () => console.log("send!");
const handleChange = (e) => {
setVal(e.target.value);
};
return (
<form onSubmit={handleSubmit(handleSubmitForm)}>
<input
type="text"
name="test"
value={val}
ref={register({ required: "The field is required." })}
onChange={handleChange}
/>
{errors.test && errors.test.message}
<button type="submit">Send</button>
</form>
);
};
When I click "Submit", react-hook-form show me error message: 'The field is required.
'. And when I want write word "TEST" and next I try remove this text by for example select all text and click Backspace button, then I can't remove this text from input, but message about empty value is showing.
How I can use react-hook-form with value and onChange event?