This commit is contained in:
a 2024-05-31 17:15:02 +02:00
parent af59182923
commit fe735d5d26

19
App.js
View File

@ -17,14 +17,14 @@ import {
Button, Button,
} from "react-native"; } from "react-native";
import { useNetInfoInstance } from "@react-native-community/netinfo"; import NetInfo from '@react-native-community/netinfo'
import { import {
useQuery, useQuery,
useMutation, useMutation,
useQueryClient, useQueryClient,
QueryClient, QueryClient,
QueryClientProvider, QueryClientProvider,
onlineManager
} from "@tanstack/react-query"; } from "@tanstack/react-query";
import { PersistQueryClientProvider } from "@tanstack/react-query-persist-client"; import { PersistQueryClientProvider } from "@tanstack/react-query-persist-client";
import { createAsyncStoragePersister } from "@tanstack/query-async-storage-persister"; import { createAsyncStoragePersister } from "@tanstack/query-async-storage-persister";
@ -42,6 +42,15 @@ const queryClient = new QueryClient({
}, },
}); });
onlineManager.setEventListener((setOnline) => {
return NetInfo.addEventListener((state) => {
setOnline(!!state.isConnected)
if (state.isConnected)
queryClient.resumePausedMutations()
console.log(123)
})
})
const asyncStoragePersister = createAsyncStoragePersister({ const asyncStoragePersister = createAsyncStoragePersister({
storage: AsyncStorage, storage: AsyncStorage,
}); });
@ -52,7 +61,8 @@ queryClient.setMutationDefaults(["addTodo"], {
mutationFn: ({ page }) => { mutationFn: ({ page }) => {
return makeCall(page); return makeCall(page);
}, },
retry: 3, retry: Infinity,
networkMode: 'offline',
}); });
@ -90,7 +100,7 @@ function App() {
const ReqButton = () => { const ReqButton = () => {
const mutation = useMutation({ mutationKey: ["addTodo"] }); const mutation = useMutation({ mutationKey: ["addTodo"] });
const query = useQuery({ queryKey: ["todos"], queryFn: () => makeCall("a") }); //const query = useQuery({ queryKey: ["todos"], queryFn: () => makeCall("a") });
return ( return (
<> <>
@ -98,7 +108,6 @@ const ReqButton = () => {
title={"Request"} title={"Request"}
onPress={() => mutation.mutate({ page: "a" })} onPress={() => mutation.mutate({ page: "a" })}
/> />
<Text>{query.data && query.data.toString() || ''}</Text>
</> </>
); );
}; };