From 734defd8137fa81944832637bde91e72aa6d570c Mon Sep 17 00:00:00 2001 From: a Date: Fri, 28 Jun 2024 13:07:29 +0200 Subject: [PATCH] a --- App.js | 64 ++++++++++++++++++---------------------------------------- 1 file changed, 20 insertions(+), 44 deletions(-) diff --git a/App.js b/App.js index 5f8baac..7ed2711 100644 --- a/App.js +++ b/App.js @@ -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() }} > @@ -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" })} /> +