-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
82 lines (72 loc) · 2.13 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React from 'react';
import {Text, View} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import NetInfo from '@react-native-community/netinfo';
import {MutationCache, onlineManager, QueryClient, useQuery} from 'react-query';
import {createAsyncStoragePersister} from 'react-query/createAsyncStoragePersister';
import {PersistQueryClientProvider} from 'react-query/persistQueryClient';
onlineManager.setEventListener(setOnline => {
return NetInfo.addEventListener(state => {
const isOnline = state.isConnected === null ? undefined : state.isConnected;
setOnline(isOnline);
});
});
const queryClient = new QueryClient({
defaultOptions: {
queries: {
cacheTime: 1000 * 60 * 60 * 24,
staleTime: 2000,
},
},
mutationCache: new MutationCache({
onSuccess: data => {
console.log('onSuccess', data);
},
onError: error => {
console.log('onError', error);
},
}),
});
const persister = createAsyncStoragePersister({
storage: AsyncStorage,
key: 'react-query-offline',
});
const Root = () => {
const [store, setStore] = React.useState<string | null | undefined>();
AsyncStorage.getItem('react-query-offline').then(setStore);
const query = useQuery({
queryKey: ['use-mock-users'],
queryFn: async () => {
console.log('fetching users');
const res = await fetch('https://jsonplaceholder.typicode.com/users');
const body = await res.json();
return body as {
id: string;
name: string;
};
},
});
return (
<View>
<Text>{JSON.stringify(query)}</Text>
<Text>{store}</Text>
</View>
);
};
const App = () => {
return (
<PersistQueryClientProvider
client={queryClient}
persistOptions={{persister, buster: '2'}}
onSuccess={() => {
console.log('persist success', Date.now());
// resume mutations after initial restore from localStorage was successful
queryClient.resumePausedMutations().then(() => {
queryClient.invalidateQueries();
});
}}>
<Root />
</PersistQueryClientProvider>
);
};
export default App;