| 2 | import { useForm } from 'react-hook-form'; |
| 3 | |
| 4 | function WatchDefaultValues() { |
| 5 | const { watch } = useForm({ |
| 6 | defaultValues: { |
| 7 | test: 'test', |
| 8 | test1: { |
| 9 | firstName: 'firstName', |
| 10 | lastName: ['lastName0', 'lastName1'], |
| 11 | deep: { |
| 12 | nest: 'nest', |
| 13 | }, |
| 14 | }, |
| 15 | 'flatName[1]': { |
| 16 | whatever: 'flat', |
| 17 | }, |
| 18 | }, |
| 19 | }); |
| 20 | |
| 21 | const all = watch(); |
| 22 | const array = watch(['test', 'flatName[1]']); |
| 23 | const singleObject = watch('test1.firstName'); |
| 24 | const arrayStringOrObject = watch(['test', 'test1.firstName']); |
| 25 | const getDeepArray = watch('test1.lastName'); |
| 26 | const singleDeepArray = watch('test1.lastName.0'); |
| 27 | |
| 28 | return ( |
| 29 | <> |
| 30 | <div id="watchAll">{JSON.stringify(all)}</div> |
| 31 | <br /> |
| 32 | <div id="array">{JSON.stringify(array)}</div> |
| 33 | <br /> |
| 34 | <div id="getArray">{JSON.stringify(getDeepArray)}</div> |
| 35 | <br /> |
| 36 | <div id="object">{JSON.stringify(arrayStringOrObject)}</div> |
| 37 | <br /> |
| 38 | <div id="single">{JSON.stringify(singleObject)}</div> |
| 39 | <br /> |
| 40 | <div id="singleDeepArray">{JSON.stringify(singleDeepArray)}</div> |
| 41 | </> |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | export default WatchDefaultValues; |