You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ba6e5b1
Browse filesBrowse the repository at this point in the historyBrowse files
**`rtk-persist`** is a lightweight, zero-dependency library that enhances Redux Toolkit's state management by adding seamless, persistent storage. It allows specified slices or reducers of your Redux state to be saved to a storage medium of your choice (like `localStorage` or `AsyncStorage`) and rehydrated on app startup.
12
12
@@ -17,13 +17,23 @@ The library works by wrapping standard Redux Toolkit functions, adding persisten
17
17
## ✨ Features
18
18
19
19
***Effortless Persistence**: Persist any Redux Toolkit slice or reducer with minimal configuration.
20
+
20
21
***Asynchronous Rehydration**: Store creation is now asynchronous, ensuring that your app only renders after the state has been fully rehydrated.
22
+
21
23
***Seamless Integration**: Designed as a drop-in replacement for RTK functions. Adding or removing persistence is as simple as changing an import.
24
+
22
25
***React Redux Integration**: Comes with a `<PersistedProvider />` and a `usePersistedStore` hook for easy integration with React applications.
26
+
23
27
***Flexible API**: Choose between a `createPersistedSlice` utility or a `createPersistedReducer` builder syntax.
28
+
24
29
***Nested State Support**: Easily persist slices or reducers that are deeply nested within your root state using a simple `nestedPath` option.
30
+
31
+
***Custom Serialization**: Use `onPersist` and `onRehydrate` to transform your state before saving and after loading.
32
+
25
33
***Storage Agnostic**: Works with any storage provider that implements a simple `getItem`, `setItem`, and `removeItem` interface.
34
+
26
35
***TypeScript Support**: Fully typed to ensure a great developer experience with path validation.
36
+
27
37
***Minimal Footprint**: Extremely lightweight with a production size under 15 KB.
A core design principle of `rtk-persist` is that it should be easy to add or remove. The API is intentionally designed to mirror Redux Toolkit's, so enabling or disabling persistence is as simple as changing an import.
'features.counter'// The nestedPath to the slice's state
248
+
{
249
+
nestedPath: 'features.counter'// The nestedPath to the slice's state
250
+
}
260
251
);
261
252
262
253
// app/store.ts
@@ -277,10 +268,74 @@ export const store = configurePersistedStore(
277
268
'my-app-id',
278
269
localStorage
279
270
);
271
+
280
272
```
281
273
282
274
<br />
283
275
276
+
## 🔬 Advanced Usage: Custom Serialization
277
+
278
+
Sometimes, you may need to transform a slice's state before it's saved to storage or after it's rehydrated. For example, you might want to store a `Date` object as an ISO string, or omit certain transient properties.
279
+
280
+
`rtk-persist` supports this through the `onPersist` and `onRehydrate` options.
281
+
282
+
### Example with `onPersist` and `onRehydrate`
283
+
284
+
Here's how you can persist a slice that contains a non-serializable value like a `Date` object.
*`onPersist` converts the `lastLogin``Date` object into an ISO string before it's written to `localStorage`.
334
+
335
+
*`onRehydrate` parses the ISO string and converts it back into a `Date` object when the state is loaded from storage.
336
+
337
+
<br />
338
+
284
339
## 🛠️ API
285
340
286
341
### `createPersistedSlice`
@@ -290,13 +345,18 @@ A wrapper around RTK's `createSlice` that adds persistence.
290
345
#### Takes
291
346
292
347
***`sliceOptions`**: The standard `CreateSliceOptions` object from Redux Toolkit.
293
-
***`nestedPath`** (optional, `string`): A dot-notation string representing the path to the slice's state from the root. Required if the slice is not at the root level.
294
348
295
-
#### Returns
349
+
***`persistenceOptions`** (optional, `object`): Configuration for persistence behavior.
350
+
351
+
*`nestedPath` (optional, `string`): A dot-notation string for the slice's state if it's nested.
352
+
353
+
*`onPersist` (optional, `function`): A function to transform state *before* it's saved.
354
+
355
+
*`onRehydrate` (optional, `function`): A function to transform state *after* it's rehydrated.
296
356
297
-
* A standard `Slice` object, enhanced with a `nestedPath` property.
357
+
#### Returns
298
358
299
-
---
359
+
* A `PersistedSlice` object, which is a standard `Slice` object enhanced with persistence properties.
300
360
301
361
### `createPersistedReducer`
302
362
@@ -305,15 +365,22 @@ A wrapper around RTK's `createReducer` that adds persistence.
305
365
#### Takes
306
366
307
367
***`name`**: A unique string to identify this reducer in storage.
368
+
308
369
***`initialState`**: The initial state for the reducer.
370
+
309
371
***`builderCallback`**: A callback that receives a `builder` object to define case reducers.
310
-
***`nestedPath`** (optional, `string`): A dot-notation string representing the path to the reducer's state. An empty string (`''`) signifies that this reducer is the root state.
311
372
312
-
#### Returns
373
+
***`persistenceOptions`** (optional, `object`): Configuration for persistence behavior.
374
+
375
+
*`nestedPath` (optional, `string`): A dot-notation string for the reducer's state. An empty string (`''`) signifies the root state.
313
376
314
-
* A standard `Reducer`function, enhanced with `reducerName` and `nestedPath` properties.
377
+
*`onPersist` (optional, `function`): A function to transform state *before* it's saved.
315
378
316
-
---
379
+
*`onRehydrate` (optional, `function`): A function to transform state *after* it's rehydrated.
380
+
381
+
#### Returns
382
+
383
+
* A `PersistedReducer` function, which is a standard `Reducer` enhanced with persistence properties.
317
384
318
385
### `configurePersistedStore`
319
386
@@ -322,16 +389,22 @@ A wrapper around RTK's `configureStore`.
322
389
#### Takes
323
390
324
391
***`storeOptions`**: The standard `ConfigureStoreOptions` object.
392
+
325
393
***`applicationId`**: A unique string that identifies the application to namespace storage keys.
394
+
326
395
***`storageHandler`**: A storage object that implements `getItem`, `setItem`, and `removeItem`.
396
+
327
397
***`persistenceOptions`** (optional): An object to control the persistence behavior:
328
-
*`rehydrationTimeout` (optional, `number`): Max time in ms to wait for rehydration. Defaults to `5000`.
398
+
399
+
*`rehydrationTimeout` (optional, `number`): Max time in ms to wait for rehydration. Defaults to `5000`.
329
400
330
401
#### Returns
331
402
332
403
* A `Promise<PersistedStore>` object, which resolves to a standard Redux store enhanced with the following methods:
333
-
***`rehydrate()`**: A function to manually trigger rehydration from storage.
334
-
***`clearPersistedState()`**: A function that clears all persisted data for the application from storage.
404
+
405
+
***`rehydrate()`**: A function to manually trigger rehydration from storage.
406
+
407
+
***`clearPersistedState()`**: A function that clears all persisted data for the application from storage.
335
408
336
409
<br />
337
410
@@ -346,5 +419,5 @@ This library was crafted from our daily experiences building modern web and mobi
346
419
## 📄 License
347
420
348
421
This project is licensed under the MIT License.
349
-
422
+
350
423
Library icon freely created from a [iconsax](https://iconsax.io/) icon and the [redux](https://redux.js.org/img/redux.svg) logo.
0 commit comments