File size: 817 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import React from 'react';
import { useForm } from 'react-hook-form';
export default function App() {
const {
register,
formState: { errors },
trigger,
handleSubmit,
} = useForm();
console.log('errors', errors);
return (
<form onSubmit={handleSubmit((data) => console.log(data))}>
<h1>validationField</h1>
<label>First name: </label>
<input {...register('firstName', { required: true })} />
<label>Last name: </label>
<input {...register('lastName', { required: true })} />
<button
type="button"
onClick={async () => {
const result = await trigger(['firstName', 'lastName']);
if (result) {
console.log('Valid input');
}
}}
>
Trigger
</button>
</form>
);
}
|