Fetcher Utility
install axios
pnpm add axios
add fetcher utility for GET
import axios from 'axios';
type FetcherArgs = {
url: string;
params?: any;
};
export const fetcher = <ResType>({ url, params }: FetcherArgs) =>
axios.get<ResType>(url, { params }).then((res) => res.data);
Data Hook with SWR
install SWR
pnpm add swr
add swr hook utility
import useSWR from 'swr';
type UseAppSWRArgs<ResType> = {
url: string;
params?: any;
fallbackData?: ResType;
isReady?: boolean;
};
export const useAppSWR = <ResType, ErrorType = any>({
url,
params,
fallbackData,
isReady = true,
}: UseAppSWRArgs<ResType>) => {
const { data, error, mutate } = useSWR<ResType, ErrorType>(
isReady ? { url, params } : null,
fetcher,
{
fallbackData,
}
);
return {
data,
isLoading: !error && !data && isReady,
isError: error,
mutate,
};
};
add data hook
// useMovieData.ts
export const useMovieData = (params?: any) =>
useAppSWR({
url: 'https://some-api-url.com/api/movies',
params,
});
call the data hook in the component
// SomeComponent.tsx
const SomeComponent = () => {
const { data, isLoading } = useMovieData();
if (isLoading) {
return <p>Loading...</p>;
}
return (
<div>
{data.results.map((item) => (
<p key={item.id}>{item.title}</p>
))}
</div>
);
};