mirror of
https://github.com/streamyfin/streamyfin.git
synced 2026-06-01 19:48:28 +01:00
feat: new useQuery specifically for react navigation to invalidate !enabled queries on screen re-mount
This commit is contained in:
32
utils/useReactNavigationQuery.ts
Normal file
32
utils/useReactNavigationQuery.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import { useFocusEffect } from "@react-navigation/core";
|
||||||
|
import {
|
||||||
|
QueryKey,
|
||||||
|
useQuery,
|
||||||
|
UseQueryOptions,
|
||||||
|
UseQueryResult,
|
||||||
|
} from "@tanstack/react-query";
|
||||||
|
import { useCallback } from "react";
|
||||||
|
|
||||||
|
export function useReactNavigationQuery<
|
||||||
|
TQueryFnData = unknown,
|
||||||
|
TError = unknown,
|
||||||
|
TData = TQueryFnData,
|
||||||
|
TQueryKey extends QueryKey = QueryKey
|
||||||
|
>(
|
||||||
|
options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>
|
||||||
|
): UseQueryResult<TData, TError> {
|
||||||
|
const useQueryReturn = useQuery(options);
|
||||||
|
|
||||||
|
useFocusEffect(
|
||||||
|
useCallback(() => {
|
||||||
|
if (
|
||||||
|
((options.refetchOnWindowFocus && useQueryReturn.isStale) ||
|
||||||
|
options.refetchOnWindowFocus === "always") &&
|
||||||
|
options.enabled !== false
|
||||||
|
)
|
||||||
|
useQueryReturn.refetch();
|
||||||
|
}, [options.enabled, options.refetchOnWindowFocus])
|
||||||
|
);
|
||||||
|
|
||||||
|
return useQueryReturn;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user