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
Copy file name to clipboardExpand all lines: README.md
+47-14Lines changed: 47 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,33 +6,41 @@
6
6
7
7
The library works by wrapping standard Redux Toolkit functions, adding persistence logic without changing the way you write your reducers or actions.
8
8
9
+
<br />
9
10
10
11
## ✨ Features
11
12
12
13
***Effortless Persistence**: Persist any Redux Toolkit slice or reducer with minimal configuration.
14
+
13
15
***Flexible API**: Choose between a `createPersistedSlice` utility or a `createPersistedReducer` builder syntax.
16
+
14
17
***Storage Agnostic**: Works with any storage provider that implements a simple `getItem`, `setItem`, and `removeItem` interface.
15
-
***Selective Persistence**: An optional filter function allows you to specify exactly which parts of a state should be persisted.
18
+
19
+
***Rehydration Lifecycle**: Use optional callbacks (`onRehydrationStart`, `onRehydrationSuccess`, `onRehydrationError`) to react to the persistence lifecycle.
20
+
16
21
***TypeScript Support**: Fully typed to ensure a great developer experience.
22
+
17
23
***Minimal Footprint**: Extremely lightweight with a production size under 10 KB.
18
24
25
+
<br />
19
26
20
27
## ⚙️ Installation
21
28
22
29
You can install `rtk-persist` using either `yarn` or `npm`:
23
30
24
-
```bash
31
+
```
25
32
yarn add rtk-persist
26
33
```
27
34
28
35
or
29
36
30
-
```bash
37
+
```
31
38
npm install --save rtk-persist
32
39
```
33
40
34
41
The package has a peer dependency on `@reduxjs/toolkit`.
35
42
43
+
<br />
36
44
37
45
## 🚀 Quick Start
38
46
@@ -46,7 +54,7 @@ This approach is best if you prefer the `createSlice` API from Redux Toolkit.
46
54
47
55
Replace `createSlice` with `createPersistedSlice`. The function accepts the same options.
48
56
49
-
```typescript
57
+
```
50
58
// features/counter/counterSlice.ts
51
59
import { createPersistedSlice } from 'rtk-persist';
52
60
import { PayloadAction } from '@reduxjs/toolkit';
@@ -81,10 +89,10 @@ This approach is ideal if you prefer the `createReducer` builder syntax.
81
89
82
90
Use `createPersistedReducer` and define your case reducers using the builder callback.
83
91
84
-
```typescript
92
+
```
85
93
// features/counter/counterReducer.ts
86
94
import { createPersistedReducer } from 'rtk-persist';
Whichever option you choose, you must use `configurePersistedStore` and provide a storage handler.
117
+
Whichever option you choose, you must use `configurePersistedStore` and provide a storage handler. The store is created synchronously, and rehydration from storage happens in the background.
110
118
111
-
```typescript
119
+
```
112
120
// app/store.ts
113
121
import { configurePersistedStore } from 'rtk-persist';
114
122
// Import your slice reducer (Option 1)
@@ -134,44 +142,69 @@ export const store = configurePersistedStore(
***`storeOptions`**: The standard `ConfigureStoreOptions` object.
182
+
164
183
***`applicationId`**: A unique string that identifies the application.
184
+
165
185
***`storageHandler`**: A storage object that implements `getItem`, `setItem`, and `removeItem`.
166
186
187
+
***`persistenceOptions`** (optional): An object to control the persistence behavior:
188
+
189
+
*`rehydrationTimeout` (optional, `number`): The maximum time in milliseconds to wait for rehydration to complete before timing out. Defaults to `5000`.
190
+
191
+
*`onRehydrationStart` (optional, `() => void`): A callback invoked when the rehydration process begins.
192
+
193
+
*`onRehydrationSuccess` (optional, `() => void`): A callback invoked when the rehydration process completes successfully.
194
+
195
+
*`onRehydrationError` (optional, `(error: unknown) => void`): A callback invoked if an error occurs during rehydration.
196
+
197
+
<br />
167
198
168
199
## ❤️ Author
169
200
170
-
This library is authored and maintained by **[Fancy Pixel srl](https://www.fancypixel.it)**.
201
+
This library is authored and maintained by [**Fancy Pixel srl**](https://www.fancypixel.it).
171
202
172
203
This library was crafted from our daily experiences building modern web and mobile applications. Contributions are welcome!
@@ -19,59 +19,40 @@ Use this project to understand the library's features or as a sandbox for testin
19
19
```
20
20
21
21
3. **Install dependencies**:
22
+
This command will also link the local`rtk-persist` library from the parent directory, as specified in`package.json`.
22
23
```bash
23
24
yarn
24
25
```
25
26
26
27
4. **Run the application**:
27
28
```bash
28
-
yarn start
29
+
yarn dev
29
30
```
30
31
31
32
***
32
33
33
34
## 🛠️ Developing and Testing Local Library Changes
34
35
35
-
If you have made changes to the `rtk-persist` library source code and want to test them in this example app without publishing to npm, follow these steps.
36
-
37
-
This workflow ensures that the example app uses your latest local code from the library.
36
+
If you have made changes to the `rtk-persist` library source code, the example app can use them directly thanks to the local file path dependency. This workflow ensures that the example app always uses your latest local code from the library.
38
37
39
38
1. **Navigate to the Library's Root Directory**:
40
-
Open a terminal and go to the root of the `rtk-persist` library, not the example app.
39
+
Open a terminal and go to the root of the `rtk-persist` library (the parent directory of `example`).
41
40
```bash
42
41
# Assuming you are in the 'example' directory
43
42
cd ..
44
43
```
45
44
46
45
2. **Build the Library**:
47
-
Compile the library's TypeScript source code into JavaScript.
46
+
After making any changes to the library's source code, you must rebuild it forthe changes to be reflectedin the example app.
48
47
```bash
49
48
yarn build
50
49
```
51
50
52
-
3. **Package the Library**:
53
-
Create a local tarball (`.tgz` file) of the library. This is what you will install in the example app.
54
-
```bash
55
-
yarn pack
56
-
```
57
-
This command will create a file like `rtk-persist-v1.0.1.tgz`in the library's root directory. Note the exact filename.
58
-
59
-
4. **Navigate Back to the Example App Directory**:
51
+
3. **Run the Example App**:
52
+
Navigate back to the example app directory and start the development server. The app will automatically use the newly built version of the library.
60
53
```bash
61
54
cd example
55
+
yarn dev
62
56
```
63
57
64
-
5. **Install the Local Library Package**:
65
-
Install the `.tgz` file you created in step 3. This will override the version from the npm registry.
66
-
```bash
67
-
# Replace the filename with the one generated in step 3
68
-
yarn add file:../rtk-persist-v1.0.1.tgz
69
-
```
70
-
71
-
6. **Run the Example App**:
72
-
Start the example application to see your local changes in action.
73
-
```bash
74
-
yarn start
75
-
```
76
-
77
-
Now, the example app is running with your modified version of the `rtk-persist` library. Repeat this process every time you want to test new changes.
58
+
Now, the example app is running with your modified version of the `rtk-persist` library. Simply repeat step 2 every time you want to test new changes.
0 commit comments