@@ -20,20 +20,39 @@ function FieldInfo({ field }: { field: AnyFieldApi }) {
20
20
)
21
21
}
22
22
23
+ class DB {
24
+ private data : { firstName : string ; lastName : string }
25
+
26
+ constructor ( ) {
27
+ this . data = { firstName : 'FirstName' , lastName : 'LastName' }
28
+ }
29
+
30
+ getData ( ) : { firstName : string ; lastName : string } {
31
+ return { ...this . data }
32
+ }
33
+
34
+ async saveUser ( value : { firstName : string ; lastName : string } ) {
35
+ this . data = value
36
+ return value
37
+ }
38
+ }
39
+
40
+ // Dummy Database to emulate server-side actions
41
+ const db = new DB ( )
42
+
23
43
export default function App ( ) {
24
- const { data, isLoading } = useQuery ( {
44
+ const { data, isLoading, refetch } = useQuery ( {
25
45
queryKey : [ 'data' ] ,
26
46
queryFn : async ( ) => {
27
47
await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) )
28
- return { firstName : 'FirstName' , lastName : 'LastName' }
48
+ return db . getData ( )
29
49
} ,
30
50
} )
31
51
32
52
const saveUserMutation = useMutation ( {
33
53
mutationFn : async ( value : { firstName : string ; lastName : string } ) => {
34
54
await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) )
35
- console . log ( value )
36
- return value
55
+ db . saveUser ( value )
37
56
} ,
38
57
} )
39
58
@@ -42,9 +61,15 @@ export default function App() {
42
61
firstName : data ?. firstName ?? '' ,
43
62
lastName : data ?. lastName ?? '' ,
44
63
} ,
45
- onSubmit : async ( { value } ) => {
64
+ onSubmit : async ( { formApi , value } ) => {
46
65
// Do something with form data
47
66
await saveUserMutation . mutateAsync ( value )
67
+
68
+ // Invalidating query to recheck fresh data
69
+ await refetch ( )
70
+
71
+ // Reset the form to start-over with a clean state
72
+ formApi . reset ( )
48
73
} ,
49
74
} )
50
75
0 commit comments