Created
January 13, 2024 10:54
-
-
Save slvssb/9703b64d4ca483dfae90205e970d6379 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import NetInfo from "@react-native-community/netinfo" | |
import { | |
QueryClient, | |
QueryClientProvider, | |
focusManager, | |
onlineManager, | |
} from "@tanstack/react-query" | |
import { useEffect } from "react" | |
import { AppState, Platform, type AppStateStatus } from "react-native" | |
const queryClient = new QueryClient({ | |
defaultOptions: { | |
mutations: { retry: false }, | |
queries: { retry: false, refetchOnMount: true, refetchOnWindowFocus: false }, | |
}, | |
}) | |
onlineManager.setEventListener(setOnline => { | |
return NetInfo.addEventListener(state => { | |
setOnline(!!state.isConnected) | |
}) | |
}) | |
function onAppStateChange(status: AppStateStatus) { | |
if (Platform.OS !== "web") { | |
focusManager.setFocused(status === "active") | |
} | |
} | |
export default function ReactQueryProvider({ children }: { children: React.ReactNode }) { | |
useEffect(() => { | |
const subscription = AppState.addEventListener("change", onAppStateChange) | |
return () => subscription.remove() | |
}, []) | |
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment