Open History to see the last commit
1---
2id: createAsyncStoragePersister
3title: createAsyncStoragePersister
4---
5β
6## Installation
7β
8This utility comes as a separate package and is available under the `'@tanstack/query-async-storage-persister'` import.
9```bash
10npm install @tanstack/query-async-storage-persister @tanstack/react-query-persist-client
11```
12or
13```bash
14pnpm add @tanstack/query-async-storage-persister @tanstack/react-query-persist-client
15```
16or
17```bash
18yarn add @tanstack/query-async-storage-persister @tanstack/react-query-persist-client
19```
20β
21## Usage
22β
23- Import the `createAsyncStoragePersister` function
24- Create a new asyncStoragePersister
25 - you can pass any `storage` to it that adheres to the `AsyncStorage` interface - the example below uses the async-storage from React Native
26- Wrap your app by using [`PersistQueryClientProvider`](../plugins/persistQueryClient.md#persistqueryclientprovider) component.
27β
28```ts
29import AsyncStorage from '@react-native-async-storage/async-storage'
30import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client'
31import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'
32β
33const queryClient = new QueryClient({
34 defaultOptions: {
35 queries: {
36 cacheTime: 1000 * 60 * 60 * 24, // 24 hours
37 },
38 },
39})
40β
41const asyncStoragePersister = createAsyncStoragePersister({
42 storage: AsyncStorage
43})
44β
45const Root = () => (
46 <PersistQueryClientProvider
47 client={queryClient}
48 persistOptions={{ persister: asyncStoragePersister }}
49 >
50 <App />
51 </PersistQueryClientProvider>
52);
53β
54export default Root;
55```
56β
57## Retries
58β
59Retries work the same as for a [SyncStoragePersister](../plugins/createSyncStoragePersister), except that they can also be asynchronous. You can also use all the predefined retry handlers.
60β
61## API
62β
63### `createAsyncStoragePersister`
64β
65Call this function to create an asyncStoragePersister that you can use later with `persistQueryClient`.
66β
67```tsx
68createAsyncStoragePersister(options: CreateAsyncStoragePersisterOptions)
69```
70β
71### `Options`
72β
73```ts
74interface CreateAsyncStoragePersisterOptions {
75 /** The storage client used for setting an retrieving items from cache */
76 storage: AsyncStorage
77 /** The key to use when storing the cache to localStorage */
78 key?: string
79 /** To avoid localStorage spamming,
80 * pass a time in ms to throttle saving the cache to disk */
81 throttleTime?: number
82 /** How to serialize the data to storage */
83 serialize?: (client: PersistedClient) => string
84 /** How to deserialize the data from storage */
85 deserialize?: (cachedString: string) => PersistedClient
86 /** How to retry persistence on error **/
87 retry?: AsyncPersistRetryer
88}
89β
90interface AsyncStorage {
91 getItem: (key: string) => Promise<string>
92 setItem: (key: string, value: string) => Promise<unknown>
93 removeItem: (key: string) => Promise<unknown>
94}
95```
96β
97The default options are:
98β
99```tsx
100{
101 key = `REACT_QUERY_OFFLINE_CACHE`,
102 throttleTime = 1000,
103 serialize = JSON.stringify,
104 deserialize = JSON.parse,
105}
106```