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

60
App.js
View File

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