knox 修订了这个 Gist . 跳至此修订
1 file changed, 20 insertions
React-Hook-Form-2.jsx(file created)
@@ -0,0 +1,20 @@ | |||
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 | + | } |
更新
更早