Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

createAsyncStoragePersister.md

107 lines3.0 KB

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```tsx
29import AsyncStorage from '@react-native-async-storage/async-storage'
30import { QueryClient } from '@tanstack/react-query'
31import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client'
32import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'
33​
34const queryClient = new QueryClient({
35 defaultOptions: {
36 queries: {
37 gcTime: 1000 * 60 * 60 * 24, // 24 hours
38 },
39 },
40})
41​
42const asyncStoragePersister = createAsyncStoragePersister({
43 storage: AsyncStorage
44})
45​
46const Root = () => (
47 <PersistQueryClientProvider
48 client={queryClient}
49 persistOptions={{ persister: asyncStoragePersister }}
50 >
51 <App />
52 </PersistQueryClientProvider>
53);
54​
55export default Root;
56```
57​
58## Retries
59​
60Retries 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.
61​
62## API
63​
64### `createAsyncStoragePersister`
65​
66Call this function to create an asyncStoragePersister that you can use later with `persistQueryClient`.
67​
68```tsx
69createAsyncStoragePersister(options: CreateAsyncStoragePersisterOptions)
70```
71​
72### `Options`
73​
74```tsx
75interface CreateAsyncStoragePersisterOptions {
76 /** The storage client used for setting an retrieving items from cache */
77 storage: AsyncStorage | undefined | null
78 /** The key to use when storing the cache to localStorage */
79 key?: string
80 /** To avoid localStorage spamming,
81 * pass a time in ms to throttle saving the cache to disk */
82 throttleTime?: number
83 /** How to serialize the data to storage */
84 serialize?: (client: PersistedClient) => string
85 /** How to deserialize the data from storage */
86 deserialize?: (cachedString: string) => PersistedClient
87 /** How to retry persistence on error **/
88 retry?: AsyncPersistRetryer
90​
91interface AsyncStorage {
92 getItem: (key: string) => Promise<string | null>
93 setItem: (key: string, value: string) => Promise<unknown>
94 removeItem: (key: string) => Promise<void>
96```
97​
98The default options are:
99​
100```tsx
102 key = `REACT_QUERY_OFFLINE_CACHE`,
103 throttleTime = 1000,
104 serialize = JSON.stringify,
105 deserialize = JSON.parse,
107```