This commit is contained in:
a 2024-06-28 13:07:29 +02:00
parent 2dd2ea6c87
commit 734defd813

64
App.js
View File

@ -1,33 +1,10 @@
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React from "react";
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
Button,
} from "react-native";
import {SafeAreaView, View, Button, } from "react-native";
import NetInfo from '@react-native-community/netinfo'
import {
useQuery,
useMutation,
useQueryClient,
QueryClient,
QueryClientProvider,
onlineManager
} from "@tanstack/react-query";
import { PersistQueryClientProvider } from "@tanstack/react-query-persist-client";
import { createAsyncStoragePersister } from "@tanstack/query-async-storage-persister";
import NetInfo from "@react-native-community/netinfo";
import {useMutation, QueryClient, onlineManager} from "@tanstack/react-query";
import {PersistQueryClientProvider} from "@tanstack/react-query-persist-client";
import {createAsyncStoragePersister} from "@tanstack/query-async-storage-persister";
import AsyncStorage from "@react-native-async-storage/async-storage";
const queryClient = new QueryClient({
@ -36,20 +13,19 @@ const queryClient = new QueryClient({
gcTime: 1000 * 60 * 60 * 24, // 24 hours
},
mutations: {
cacheTime: Infinity,
retry: true,
cacheTime: 1000 * 60 * 60 * 24 * 2, // 2 days
},
},
});
onlineManager.setEventListener((setOnline) => {
return NetInfo.addEventListener((state) => {
setOnline(!!state.isConnected)
setOnline(!!state.isConnected);
if (state.isConnected)
queryClient.resumePausedMutations()
console.log(123)
})
})
queryClient.resumePausedMutations();
});
});
const asyncStoragePersister = createAsyncStoragePersister({
storage: AsyncStorage,
@ -62,19 +38,19 @@ queryClient.setMutationDefaults(["addTodo"], {
return makeCall(page);
},
retry: Infinity,
networkMode: 'offline',
networkMode: "offline",
});
function makeCall(page) {
console.log(queryClient.getMutationCache().getAll().length)
return fetch(`http://10.0.2.2:8080/${page}`)
console.log('mutations: ' + queryClient.getMutationCache().getAll().length);
console.log(queryClient.getMutationCache().getAll());
return fetch(`http://192.168.178.42:8080/${page}`)
.then(response => {
return response;
})
.catch(error => {
console.log(error);
console.log(12);
throw error;
});
}
@ -84,9 +60,8 @@ function App() {
client={queryClient}
persistOptions={{ persister: asyncStoragePersister }}
onSuccess={() => {
queryClient.resumePausedMutations().then(() => {
queryClient.invalidateQueries();
});
// resume mutations after initial restore from localStorage was successful
queryClient.resumePausedMutations()
}}
>
<SafeAreaView>
@ -101,7 +76,6 @@ function App() {
const ReqButton = () => {
const mutation = useMutation({ mutationKey: ["addTodo"] });
//const query = useQuery({ queryKey: ["todos"], queryFn: () => makeCall("a") });
return (
<>
@ -109,6 +83,8 @@ const ReqButton = () => {
title={"Request"}
onPress={() => mutation.mutate({ page: "a" })}
/>
<Button title={'Resume'} onPress={() => queryClient.resumePausedMutations()}/>
<Button title={'clear'} onPress={() => queryClient.getMutationCache().clear()}/>
</>
);
};