|
| 1 | +--- |
| 2 | +id: createSessionStoragePersistor |
| 3 | +title: createSessionStoragePersistor (Experimental) |
| 4 | +--- |
| 5 | + |
| 6 | +> VERY IMPORTANT: This utility is currently in an experimental stage. This means that breaking changes will happen in minor AND patch releases. Use at your own risk. If you choose to rely on this in production in an experimental stage, please lock your version to a patch-level version to avoid unexpected breakages. |
| 7 | +
|
| 8 | +## Installation |
| 9 | + |
| 10 | +This utility comes packaged with `react-query` and is available under the `react-query/createSessionStoragePersistor-experimental` import. |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +- Import the `createSessionStoragePersistor` function |
| 15 | +- Create a new sessionStoragePersistor |
| 16 | +- Pass it to the [`persistQueryClient`](../persistQueryClient) function |
| 17 | + |
| 18 | +```ts |
| 19 | +import { persistQueryClient } from 'react-query/persistQueryClient-experimental' |
| 20 | +import { createSessionStoragePersistor } from 'react-query/createSessionStoragePersistor-experimental' |
| 21 | + |
| 22 | +const queryClient = new QueryClient({ |
| 23 | + defaultOptions: { |
| 24 | + queries: { |
| 25 | + cacheTime: 1000 * 60 * 60 * 24, // 24 hours |
| 26 | + }, |
| 27 | + }, |
| 28 | +}) |
| 29 | + |
| 30 | +const sessionStoragePersistor = createSessionStoragePersistor() |
| 31 | + |
| 32 | +persistQueryClient({ |
| 33 | + queryClient, |
| 34 | + persistor: sessionStoragePersistor, |
| 35 | +}) |
| 36 | +``` |
| 37 | + |
| 38 | +## API |
| 39 | + |
| 40 | +### `createSessionStoragePersistor` |
| 41 | + |
| 42 | +Call this function (with an optional options object) to create a sessionStoragePersistor that you can use later with `persisteQueryClient`. |
| 43 | + |
| 44 | +```js |
| 45 | +createSessionStoragePersistor(options?: CreateSessionStoragePersistorOptions) |
| 46 | +``` |
| 47 | + |
| 48 | +### `Options` |
| 49 | + |
| 50 | +An optional object of options: |
| 51 | + |
| 52 | +```js |
| 53 | +interface CreateSessionStoragePersistorOptions { |
| 54 | + /** The key to use when storing the cache to sessionstorage */ |
| 55 | + sessionStorageKey?: string |
| 56 | + /** To avoid sessionstorage spamming, |
| 57 | + * pass a time in ms to throttle saving the cache to disk */ |
| 58 | + throttleTime?: number |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +The default options are: |
| 63 | + |
| 64 | +```js |
| 65 | +{ |
| 66 | + sessionStorageKey = `REACT_QUERY_OFFLINE_CACHE`, |
| 67 | + throttleTime = 1000, |
| 68 | +} |
| 69 | +``` |
0 commit comments