fix(striker-ui): expose mutate function in useFetch hook

main
Tsu-ba-me 11 months ago
parent 917e57dac4
commit 87f69dd845
  1. 11
      striker-ui/hooks/useFetch.tsx

@ -1,5 +1,5 @@
import { useMemo } from 'react';
import useSWR, { BareFetcher, SWRConfiguration } from 'swr';
import useSWR, { BareFetcher, KeyedMutator, SWRConfiguration } from 'swr';
import API_BASE_URL from '../lib/consts/API_BASE_URL';
@ -8,6 +8,7 @@ import fetchJSON from '../lib/fetchers/fetchJSON';
type FetchHookResponse<D, E extends Error = Error> = {
data?: D;
error?: E;
mutate: KeyedMutator<D>;
loading: boolean;
};
@ -26,7 +27,11 @@ const useFetch = <Data, Alt = Data>(
...config
} = options;
const { data, error } = useSWR<Data>(`${baseUrl}${url}`, fetcher, config);
const { data, error, mutate } = useSWR<Data>(
`${baseUrl}${url}`,
fetcher,
config,
);
const altData = useMemo<Alt | undefined>(
() => mod && data && mod(data),
@ -35,7 +40,7 @@ const useFetch = <Data, Alt = Data>(
const loading = !error && !data;
return { altData, data, error, loading };
return { altData, data, error, mutate, loading };
};
export default useFetch;

Loading…
Cancel
Save