Skip to content

Commit cc8bbb5

Browse files
committed
docs: add docs about persistence for QueryClient
1 parent a038d3e commit cc8bbb5

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

docs/api/Query.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,4 @@ export const queryClient = new QueryClient({
297297
queryClient.mount();
298298
```
299299

300-
If you work with [`QueryClient`](/api/QueryClient) then calling `mount()` is not needed.
300+
If you work with [`QueryClient`](/api/QueryClient) then calling `mount()` is not needed.

docs/api/QueryClient.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,55 @@ const data = client.getQueryData(['todos']);
9191
Use `QueryClient` if you need:
9292
- Customization of query/mutation lifecycle
9393
- Tracking entity initialization/destruction events
94-
- Advanced configuration for `MobX`-powered queries and mutations.
94+
- Advanced configuration for `MobX`-powered queries and mutations.
95+
96+
97+
## Persistence
98+
99+
If you need persistence you can use built-in TanStack query feature like `createSyncStoragePersister` or `createAsyncStoragePersister`
100+
Follow this guide from original TanStack query documentation:
101+
https://tanstack.com/query/latest/docs/framework/react/plugins/createSyncStoragePersister#api
102+
103+
104+
### Example of implementation
105+
106+
1. Install TanStack's persister dependencies:
107+
108+
::: code-group
109+
110+
```bash [npm]
111+
npm install @tanstack/query-async-storage-persister @tanstack/react-query-persist-client
112+
```
113+
114+
```bash [pnpm]
115+
pnpm add @tanstack/query-async-storage-persister @tanstack/react-query-persist-client
116+
```
117+
118+
```bash [yarn]
119+
yarn add @tanstack/query-async-storage-persister @tanstack/react-query-persist-client
120+
```
121+
122+
:::
123+
124+
2. Create `QueryClient` instance and attach "persistence" feature to it
125+
126+
```ts{2,3,4,6,10}
127+
import { QueryClient } from "mobx-tanstack-query";
128+
import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister';
129+
import { persistQueryClient } from '@tanstack/react-query-persist-client';
130+
import { compress, decompress } from 'lz-string'
131+
132+
export const queryClient = new QueryClient({
133+
defaultOptions: { queries: { staleTime: Infinity } },
134+
})
135+
136+
persistQueryClient({
137+
queryClient: queryClient as any,
138+
persister: createAsyncStoragePersister({
139+
storage: window.localStorage,
140+
serialize: (data) => compress(JSON.stringify(data)),
141+
deserialize: (data) => JSON.parse(decompress(data)),
142+
}),
143+
maxAge: Infinity,
144+
});
145+
```

0 commit comments

Comments
 (0)