Skip to content

Instantly share code, notes, and snippets.

@mgtitimoli
Created December 22, 2020 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgtitimoli/231e1efae8f857db6fd8c80648acb98d to your computer and use it in GitHub Desktop.
Save mgtitimoli/231e1efae8f857db6fd8c80648acb98d to your computer and use it in GitHub Desktop.
useNonCachedQuery
import {useEffect} from "react";
import {useQuery, useQueryClient} from "react-query";
import type {
QueryKey,
QueryFunction,
UseQueryOptions,
UseQueryResult
} from "react-query";
const useNonCachedQuery = <
TData = unknown,
TError = unknown,
TQueryFnData = TData
>(
queryKey: QueryKey,
queryFn: QueryFunction<TQueryFnData | TData>,
options?: UseQueryOptions<TData, TError, TQueryFnData>
): UseQueryResult<TData, TError> => {
const queryClient = useQueryClient();
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => () => queryClient.removeQueries(queryKey), []);
return useQuery(queryKey, queryFn, options);
};
export default useNonCachedQuery;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment