Skip to content

Commit 78a1e8a

Browse files
docs: clarify setting cacheTime for persistWithLocalStorage to work properly (#1612)
1 parent e3f0ad5 commit 78a1e8a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

docs/src/pages/plugins/persist-localstorage.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,30 @@ This plugin comes packaged with `react-query` and is available under the `react-
1111

1212
## Usage
1313

14-
Import the `persistWithLocalStorage` function, and pass it your `QueryClient` instance!
14+
Import the `persistWithLocalStorage` function, and pass it your `QueryClient` instance (with a `cacheTime` set)!
1515

1616
```js
1717
import { persistWithLocalStorage } from 'react-query/persist-localstorage-experimental'
1818

19-
const queryClient = new QueryClient()
19+
const queryClient = new QueryClient({
20+
defaultOptions: {
21+
queries: {
22+
cacheTime: 1000 * 60 * 60 * 24 // 24 hours
23+
}
24+
}
25+
})
2026

2127
persistWithLocalStorage(queryClient)
2228
```
2329

30+
**IMPORTANT** - for persist to work properly, you need to pass `QueryClient` a `cacheTime` value to override the default during hydration (as shown above).
31+
32+
If it is not set when creating the `QueryClient` instance, it will default to `300000` (5 minutes) for hydration, and local storage will be discarded after 5 minutes of inactivity. This is the default garbage collection behavior.
33+
34+
It should be set as the same value or higher than persistWithLocalStorage's `maxAge` option. E.g. if `maxAge` is 24 hours (the default) then `cacheTime` should be 24 hours or higher. If lower than `maxAge`, garbage collection will kick in and discard the local storage earlier than expected.
35+
36+
You can also pass it `Infinity` to disable garbage collection behavior entirely.
37+
2438
## How does it work?
2539

2640
As you use your application:

0 commit comments

Comments
 (0)