react-native-template-by-nh
Version:
React Native Template for a quick start with TypeScript using best existing libraries and scalable structure
28 lines (25 loc) • 748 B
text/typescript
import ReactQueryKeys from '@constants/reactQueryKeys';
import axiosInstance from '@lib/axios';
import {QueryKey, useQuery, UseQueryOptions} from '@tanstack/react-query';
type Output = Array<{userIde: number; id: number; title: string; body: string}>;
type OutputError = ErrorConstructor;
export const useGetPosts = (
options: Omit<
UseQueryOptions<Output, OutputError, Output, QueryKey>,
'queryKey' | 'queryFn'
> = {},
) => {
return useQuery<Output, OutputError>(
[ReactQueryKeys.Posts],
() =>
axiosInstance
.get<Output>('posts')
.then(response => {
return response?.data;
})
.catch(error => {
throw new Error(error?.message);
}),
{...options},
);
};