react-query-est/App.js

93 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-05-31 16:19:51 +02:00
/**
* 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
},
},
});
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 }}
>
<SafeAreaView>
<View>
<ReqButton />
</View>
</SafeAreaView>
</PersistQueryClientProvider>
);
}
const ReqButton = () => {
const mutation = useMutation({ mutationKey: ["addTodo"] });
return (
<Button
title={"Request"}
onPress={() => mutation.mutate({page: "a" })}
/>
);
};
export default App;