| 2 | import { useForm, Controller } from 'react-hook-form'; |
| 3 | |
| 4 | const SetValueAsyncStrictMode = () => { |
| 5 | const state = React.useRef(new Set()); |
| 6 | const [, update] = React.useState({}); |
| 7 | const { register, setValue, control } = useForm<{ |
| 8 | firstName: string; |
| 9 | }>(); |
| 10 | |
| 11 | useEffect(() => { |
| 12 | setTimeout(() => { |
| 13 | setValue('firstName', 'A'); |
| 14 | }, 10); |
| 15 | |
| 16 | setTimeout(() => { |
| 17 | setValue('firstName', 'B', { shouldDirty: true }); |
| 18 | }, 20); |
| 19 | |
| 20 | setTimeout(() => { |
| 21 | setValue('firstName', 'C', { shouldTouch: true }); |
| 22 | }, 30); |
| 23 | |
| 24 | setTimeout(() => { |
| 25 | setValue('firstName', 'D', { shouldValidate: true }); |
| 26 | }, 40); |
| 27 | }, [register, setValue]); |
| 28 | |
| 29 | return ( |
| 30 | <React.StrictMode> |
| 31 | <form> |
| 32 | <Controller |
| 33 | defaultValue={'test'} |
| 34 | control={control} |
| 35 | render={({ field }) => { |
| 36 | state.current.add(field.value); |
| 37 | return <input id={'input'} {...field} />; |
| 38 | }} |
| 39 | name={'firstName'} |
| 40 | /> |
| 41 | |
| 42 | <button id="submit" type={'button'} onClick={() => update({})}> |
| 43 | Submit |
| 44 | </button> |
| 45 | |
| 46 | <p id="result">{JSON.stringify([...state.current])}</p> |
| 47 | </form> |
| 48 | </React.StrictMode> |
| 49 | ); |
| 50 | }; |
| 51 | |
| 52 | export default SetValueAsyncStrictMode; |