React-Hook-Form-2.jsx
· 548 B · react
Brut
import { useForm } from 'react-hook-form';
function App() {
const {
register,
handleSubmit,
formState: { errors },
} = useForm();
return (
<form onSubmit={handleSubmit((data) => console.log(data))}>
<input {...register('firstName')} />
<input {...register('lastName', { required: true })} />
{errors.lastName && <p>Last name is required.</p>}
<input {...register('age', { pattern: /\d+/ })} />
{errors.age && <p>Please enter number for age.</p>}
<input type="submit" />
</form>
);
}
1 | import { useForm } from 'react-hook-form'; |
2 | |
3 | function App() { |
4 | const { |
5 | register, |
6 | handleSubmit, |
7 | formState: { errors }, |
8 | } = useForm(); |
9 | |
10 | return ( |
11 | <form onSubmit={handleSubmit((data) => console.log(data))}> |
12 | <input {...register('firstName')} /> |
13 | <input {...register('lastName', { required: true })} /> |
14 | {errors.lastName && <p>Last name is required.</p>} |
15 | <input {...register('age', { pattern: /\d+/ })} /> |
16 | {errors.age && <p>Please enter number for age.</p>} |
17 | <input type="submit" /> |
18 | </form> |
19 | ); |
20 | } |