Skip to content

Commit

Permalink
chore: update how we use swr
Browse files Browse the repository at this point in the history
  • Loading branch information
toanbku committed Jan 16, 2023
1 parent 5e07018 commit 919fbc0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion contexts/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const GameContextProvider = ({ children }: PropsWithChildren) => {

const {
data,
loading: isLoadingGameState,
isLoading: isLoadingGameState,
mutate: mutateGameState,
} = useFetchWithCache(
[GET_PATHS.gameDetail(gameId)],
Expand Down
9 changes: 6 additions & 3 deletions hooks/useFetchWithCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ export function useFetchWithCache<Data = any, Error = any>(
fn: Fetcher<Data> | null = null,
config?: SWRConfiguration<Data, Error>
) {
const { data, error, ...rest } = useSWR<Data, Error>(key, fn, config);
const { data, error, isLoading, ...rest } = useSWR<Data, Error>(
key,
fn,
config
);
const [internalData, setInternalData] = useState<Data>();

const isFirstLoading = !internalData && !error;
const loading = !data && !error;

useEffect(() => {
if (data) {
setInternalData(data);
}
}, [data]);

return { data: internalData, isFirstLoading, loading, error, ...rest };
return { data: internalData, isFirstLoading, isLoading, error, ...rest };
}

0 comments on commit 919fbc0

Please sign in to comment.