Skip to content

Commit bcc3606

Browse files
committed
Persist the cache
1 parent 7d1d143 commit bcc3606

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

App.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
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,
1621
})
1722

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

0 commit comments

Comments
 (0)