Skip to content

Instantly share code, notes, and snippets.

@slvssb
Created January 13, 2024 10:54
Show Gist options
  • Save slvssb/9703b64d4ca483dfae90205e970d6379 to your computer and use it in GitHub Desktop.
Save slvssb/9703b64d4ca483dfae90205e970d6379 to your computer and use it in GitHub Desktop.
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