Open History to see the last commit
1// From: https://github.com/TanStack/query/issues/6204
2export function useDetails({ groupId }: { groupId?: string }): any {
3 const cacheKey = ["details", groupId];
4 const query = () => fetch(`/group/${groupId}`);
5 const queryObject = useQuery(cacheKey, query, {
6 enabled: !!groupId,
7 });
8 return queryObject;
9}
10β
11// Based on: https://github.com/TanStack/query/issues/6204
12export function useDetailsContainsIdentifierAsThirdArgument({ groupId }: { groupId?: string }): any {
13 const cacheKey = ["details", groupId];
14 const query = () => fetch(`/group/${groupId}`);
15 const opts = { enabled: !!groupId }
16 const queryObject = useQuery(cacheKey, query, opts);
17 return queryObject;
18}
19β
20// From: https://twitter.com/samcook_/status/1715063150184132902
21export function useWhatever({ thing }: { thing: string }) {
22 return useQuery(
23 ['some-string', someVar],
24 async () => 'foo',
25 { enabled: Boolean(thing) },
26 );
27}
28β
29// From: https://github.com/TanStack/query/issues/6548
30export function useDeleteSomething(): any {
31 return useMutation(({ groupId }: { groupId: string }) => {
32 return fetch(`/api/groups/${groupId}`, {
33 method: 'DELETE',
34 });
35 });
36}
37β
38// From: https://github.com/TanStack/query/issues/6548
39export function useDeleteSomethingWithOnError(): any {
40 return useMutation(
41 ({ groupId }: { groupId: string }) => {
42 return fetch(`/api/groups/${groupId}`, {
43 method: 'DELETE',
44 });
45 },
46 {
47 onError: (_error, _variables, context) => {
48 // An error happened!
49 console.log(
50 `rolling back optimistic delete with id ${context.id}`
51 );
52 },
53 }
54 );
55}
56β