107 lines
2.1 KiB
JavaScript
107 lines
2.1 KiB
JavaScript
/**
|
|
* 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 { useNetInfoInstance } from "@react-native-community/netinfo";
|
|
|
|
import {
|
|
useQuery,
|
|
useMutation,
|
|
useQueryClient,
|
|
QueryClient,
|
|
QueryClientProvider,
|
|
} 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({
|
|
defaultOptions: {
|
|
queries: {
|
|
gcTime: 1000 * 60 * 60 * 24, // 24 hours
|
|
},
|
|
mutations: {
|
|
cacheTime: Infinity,
|
|
retry: true,
|
|
},
|
|
},
|
|
});
|
|
|
|
const asyncStoragePersister = createAsyncStoragePersister({
|
|
storage: AsyncStorage,
|
|
});
|
|
|
|
|
|
// Define the "addTodo" mutation
|
|
queryClient.setMutationDefaults(["addTodo"], {
|
|
mutationFn: ({ page }) => {
|
|
return makeCall(page);
|
|
},
|
|
retry: 3,
|
|
});
|
|
|
|
|
|
function makeCall(page) {
|
|
return fetch(`http://10.0.2.2:8080/${page}`)
|
|
.then(response => {
|
|
return response;
|
|
})
|
|
.catch(error => {
|
|
console.log(error);
|
|
console.log(12);
|
|
});
|
|
}
|
|
|
|
function App() {
|
|
return (
|
|
<PersistQueryClientProvider
|
|
client={queryClient}
|
|
persistOptions={{ persister: asyncStoragePersister }}
|
|
onSuccess={() => {
|
|
queryClient.resumePausedMutations().then(() => {
|
|
queryClient.invalidateQueries();
|
|
});
|
|
}}
|
|
>
|
|
<SafeAreaView>
|
|
<View>
|
|
<ReqButton />
|
|
</View>
|
|
</SafeAreaView>
|
|
</PersistQueryClientProvider>
|
|
|
|
);
|
|
}
|
|
|
|
const ReqButton = () => {
|
|
const mutation = useMutation({ mutationKey: ["addTodo"] });
|
|
const query = useQuery({ queryKey: ["todos"], queryFn: () => makeCall("a") });
|
|
|
|
return (
|
|
<>
|
|
<Button
|
|
title={"Request"}
|
|
onPress={() => mutation.mutate({ page: "a" })}
|
|
/>
|
|
<Text>{query.data && query.data.toString() || ''}</Text>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default App;
|