@@ -91,4 +91,55 @@ const data = client.getQueryData(['todos']);
9191Use ` 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