Skip to content

Commit 4a7f30c

Browse files
committed
Persist the cache
1 parent 7d1d143 commit 4a7f30c

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

App.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
1-
import React from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import { StatusBar } from 'expo-status-bar'
33
import { NavigationContainer } from '@react-navigation/native'
44
import { createStackNavigator } from '@react-navigation/stack'
55
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client'
6+
import AsyncStorage from '@react-native-community/async-storage'
7+
import { persistCache } from 'apollo3-cache-persist'
8+
import { AppLoading } from 'expo'
69

710
import HomeScreen from './src/HomeScreen'
811
import ChapterScreen from './src/ChapterScreen'
912
import { screenOptions } from './src/styles'
1013

1114
const Stack = createStackNavigator()
1215

16+
const cache = new InMemoryCache()
17+
1318
const client = new ApolloClient({
1419
uri: 'https://api.graphql.guide/graphql',
15-
cache: new InMemoryCache(),
20+
cache,
21+
defaultOptions: { watchQuery: { fetchPolicy: 'cache-and-network' } },
1622
})
1723

1824
export default function App() {
25+
const [loadingCache, setLoadingCache] = useState(true)
26+
27+
useEffect(() => {
28+
persistCache({
29+
cache,
30+
storage: AsyncStorage,
31+
}).then(() => setLoadingCache(false))
32+
}, [])
33+
34+
if (loadingCache) {
35+
return <AppLoading />
36+
}
37+
1938
return (
2039
<ApolloProvider client={client}>
2140
<NavigationContainer>

0 commit comments

Comments
 (0)