@@ -17,7 +17,7 @@ import { slowFormRequest } from '@/utils/slow-form-request'
1717
1818export const HookForm : FC = ( ) => {
1919 const [ isPending , startTransition ] = useTransition ( )
20- const [ optimisticState , addOptimisticState ] = useOptimistic < string [ ] > ( [ ] )
20+ const [ optimisticState , addOptimisticState ] = useOptimistic < string > ( '' )
2121 const {
2222 register,
2323 handleSubmit,
@@ -36,17 +36,17 @@ export const HookForm: FC = () => {
3636 const handleFormSubmit = ( data : FormSchema ) => {
3737 startTransition ( async ( ) => {
3838 try {
39- addOptimisticState ( [ 'Submitting form...' ] )
39+ addOptimisticState ( 'Submitting form...' )
4040
4141 await slowFormRequest ( data )
4242
43- addOptimisticState ( [ 'Form submitted successfully!' ] )
43+ addOptimisticState ( 'Form submitted successfully!' )
4444 // Keep the optimistic state for 1 second
4545 await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) )
4646
4747 reset ( )
4848 } catch ( error ) {
49- addOptimisticState ( [ `Form submission failed! ${ JSON . stringify ( error ) } ` ] )
49+ addOptimisticState ( `Form submission failed! ${ JSON . stringify ( error ) } ` )
5050 }
5151 } )
5252 }
@@ -63,9 +63,7 @@ export const HookForm: FC = () => {
6363 </ FieldWrapper >
6464 ) ) }
6565
66- { optimisticState . map ( ( state , index ) => (
67- < p key = { index } > { state } </ p >
68- ) ) }
66+ { optimisticState && < div > { optimisticState } </ div > }
6967
7068 < Button disabled = { isPending } > Submit</ Button >
7169 </ Form >
0 commit comments