Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache persistence based on AsyncStorage #265

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions pages/docs/advanced/react-native.en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,64 @@ import { AppState } from 'react-native'
```

For `initReconnect`, it requires some 3rd party libraries such as [NetInfo](https://github.com/react-native-netinfo/react-native-netinfo) to subscribe to the network status. The implementation will be similar to the example above: receiving a `callback` function and trigger it when the network recovers from offline, so SWR can start a revalidation to keep your data up-to-date.


### AsyncStorage Based Persistent Cache

You might want to sync your cache to `AsyncStorage`. Here's an example implementation:

```jsx
const useAsyncStorage = () => {
const [cache, setCache] = useState(null)

useEffect(() => {
// When initializing, we restore the data from `AsyncStorage` into a map.
const getCache = async () => {
const appCache = await AsyncStorage.getItem('app-cache') || '[]'
// We still use the map for write & read for performance.
setCache(new Map(JSON.parse(appCache)))
}

getCache()
}, [])

useEffect(() => {
// Before closing the application, we write all the data to `AsyncStorage`
// and keep your cache up to date.
const subscription = AppState.addEventListener('change', nextAppState => {
if (nextAppState.match(/inactive|background/)) {
const appCache = JSON.stringify(Array.from(cache.entries()))
AsyncStorage.setItem('app-cache', appCache)
}
})

return () => {
subscription.remove()
}
}, [cache])

return cache
}

const App = () => {
const cache = useAsyncStorage()

if (cache) {
return (
<SWRConfig
value={{ provider: () => cache }}
>
<NavigationContainer>
<HomeStack />
</NavigationContainer>
</SWRConfig>
)
}

return null
}
```

<Callout>
Note that this example requires the cache provider to be a Map instance.
</Callout>