fix : default mutation added

This commit is contained in:
Dishank-Kheni 2024-05-27 13:39:31 +02:00
parent 93e90eaf3f
commit d34673a490
2 changed files with 21 additions and 23 deletions

View File

@ -5,31 +5,31 @@ import NetInfo from '@react-native-community/netinfo';
import AsyncStorage from '@react-native-async-storage/async-storage'; import AsyncStorage from '@react-native-async-storage/async-storage';
import useOfflineMutation from './useOfflineMutation'; import useOfflineMutation from './useOfflineMutation';
const retryOfflineRequests = async (mutation) => { //const retryOfflineRequests = async (mutation) => {
const pendingRequest = await AsyncStorage.getItem('pendingRequest'); // const pendingRequest = await AsyncStorage.getItem('pendingRequest');
if (pendingRequest) { // if (pendingRequest) {
mutation.mutate(JSON.parse(pendingRequest)); // mutation.mutate(JSON.parse(pendingRequest));
} // }
}; //};
const MyComponent = () => { const MyComponent = () => {
const mutation = useOfflineMutation(); const mutation = useOfflineMutation();
// Retry pending requests on component mount // Retry pending requests on component mount
useEffect(() => { // useEffect(() => {
retryOfflineRequests(mutation); // retryOfflineRequests(mutation);
//
// Add event listener for when the app comes back online // // Add event listener for when the app comes back online
const unsubscribe = NetInfo.addEventListener(state => { // const unsubscribe = NetInfo.addEventListener(state => {
if (state.isConnected) { // if (state.isConnected) {
retryOfflineRequests(mutation); // retryOfflineRequests(mutation);
} // }
}); // });
//
return () => { // return () => {
unsubscribe(); // unsubscribe();
}; // };
}, [mutation]); // }, [mutation]);
const handleSubmit = (data) => { const handleSubmit = (data) => {
mutation.mutate(data); mutation.mutate(data);
@ -38,8 +38,6 @@ const MyComponent = () => {
return ( return (
<View> <View>
<Button onPress={() => handleSubmit({ title: 'New Todo' })} title="Add Todo" /> <Button onPress={() => handleSubmit({ title: 'New Todo' })} title="Add Todo" />
{mutation.isLoading && <Text>Loading...</Text>}
{mutation.isError && <Text>Error: {mutation.error.message}</Text>}
</View> </View>
); );
}; };

View File

@ -6,7 +6,7 @@ import { sendRequest } from './api';
const useOfflineMutation = () => { const useOfflineMutation = () => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const mutation = useMutation(sendRequest, { const mutation = queryClient.setMutationDefaults(sendRequest, {
onMutate: async (newData) => { onMutate: async (newData) => {
await queryClient.cancelQueries('data'); await queryClient.cancelQueries('data');