File tree 1 file changed +29
-0
lines changed
docs/framework/react/reference
1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,35 @@ const data = useMutationState({
36
36
filters: { mutationKey },
37
37
select : (mutation ) => mutation .state .data ,
38
38
})
39
+
40
+ ```
41
+
42
+ ** Example 3: Access the latest mutation data via the ` mutationKey ` **
43
+ Each invocation of ` mutate ` adds a new entry to the mutation cache for ` gcTime ` milliseconds.
44
+
45
+ To access the latest invocation, you can check for the last item that ` useMutationState ` returns.
46
+
47
+ ``` tsx
48
+ import { useMutation , useMutationState } from ' @tanstack/react-query'
49
+
50
+ const mutationKey = [' posts' ]
51
+
52
+ // Some mutation that we want to get the state for
53
+ const mutation = useMutation ({
54
+ mutationKey ,
55
+ mutationFn : (newPost ) => {
56
+ return axios .post (' /posts' , newPost )
57
+ },
58
+ })
59
+
60
+ const data = useMutationState ({
61
+ // this mutation key needs to match the mutation key of the given mutation (see above)
62
+ filters: { mutationKey },
63
+ select : (mutation ) => mutation .state .data ,
64
+ })
65
+
66
+ // Latest mutation data
67
+ const latest = data [data .length - 1 ]
39
68
```
40
69
41
70
** Options**
You can’t perform that action at this time.
0 commit comments