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

createAsyncStoragePersister.md

106 lines2.9 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```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
89​
90interface AsyncStorage {
91 getItem: (key: string) => Promise<string>
92 setItem: (key: string, value: string) => Promise<unknown>
93 removeItem: (key: string) => Promise<unknown>
95```
96​
97The default options are:
98​
99```tsx
101 key = `REACT_QUERY_OFFLINE_CACHE`,
102 throttleTime = 1000,
103 serialize = JSON.stringify,
104 deserialize = JSON.parse,
106```