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