createWebStoragePersister

Installation

This utility comes packaged with react-query and is available under the react-query/createWebStoragePersister import.

Usage

  • Import the createWebStoragePersister function
  • Create a new webStoragePersister
  • Pass it to the persistQueryClient function
import { persistQueryClient } from 'react-query/persistQueryClient'
import { createWebStoragePersister } from 'react-query/createWebStoragePersister'
const queryClient = new QueryClient({
defaultOptions: {
queries: {
cacheTime: 1000 * 60 * 60 * 24, // 24 hours
},
},
})
const localStoragePersister = createWebStoragePersister({ storage: window.localStorage })
// const sessionStoragePersister = createWebStoragePersister({ storage: window.sessionStorage })
persistQueryClient({
queryClient,
persister: localStoragePersister,
})

API

createWebStoragePersister

Call this function to create a webStoragePersister that you can use later with persistQueryClient.

createWebStoragePersister(options: CreateWebStoragePersisterOptions)

Options

interface CreateWebStoragePersisterOptions {
/** The storage client used for setting an retrieving items from cache (window.localStorage or window.sessionStorage) */
storage: Storage
/** The key to use when storing the cache */
key?: string
/** To avoid spamming,
* pass a time in ms to throttle saving the cache to disk */
throttleTime?: number
/** How to serialize the data to storage */
serialize?: (client: PersistedClient) => string
/** How to deserialize the data from storage */
deserialize?: (cachedString: string) => PersistedClient
}

The default options are:

{
key = `REACT_QUERY_OFFLINE_CACHE`,
throttleTime = 1000,
serialize = JSON.stringify,
deserialize = JSON.parse,
}
Was this page helpful?

Resources

Subscribe to our newsletter

The latest TanStack news, articles, and resources, sent to your inbox.

    I won't send you spam.

    Unsubscribe at any time.

    © 2020 Tanner Linsley. All rights reserved.