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
Redux Persist takes your redux state object and saves it to persisted storage. On app launch, it retrieves this persisted state and saves it back to redux.
8
-
9
-
**Note:** These instructions are for redux-persist v5. For a list of breaking changes between v4 and v5, see our [migration guide](./docs/MigrationGuide-v5.md).
10
-
[v4](https://github.com/rt2zz/redux-persist/tree/v4) will be supported for the forseeable future, and if it works well for your use case you are encouraged to stay on v4.
11
-
12
7
## Quickstart
13
8
`npm install redux-persist`
14
9
@@ -26,13 +21,13 @@ Basic usage involves adding `persistReducer` and `persistStore` to your setup. *
If you are using react, wrap your root component with [PersistGate](./docs/PersistGate.md). This delays the rendering of your app's UI until your persisted state has been retrieved and saved to redux. **NOTE** the `PersistGate` loading prop can be null, or any react instance, e.g. `loading={<Loading />}`
// ... normal setup, create store and persistor, import components etc.
59
48
60
49
constApp= () => {
61
50
return (
@@ -66,8 +55,6 @@ const App = () => {
66
55
</Provider>
67
56
);
68
57
};
69
-
70
-
exportdefaultApp
71
58
```
72
59
73
60
## API
@@ -91,33 +78,33 @@ export default App
91
78
92
79
#### `persistor object`
93
80
- the persistor object is returned by persistStore with the following methods:
94
-
-`.purge(keys)`
81
+
-`.purge()`
95
82
- purges state from disk and returns a promise
96
-
-`flush()`
83
+
-`.flush()`
97
84
- immediately writes all pending state to disk and returns a promise
98
-
-`pause()`
85
+
-`.pause()`
99
86
- pauses persistence
100
-
-`persist()`
87
+
-`.persist()`
101
88
- resumes persistence
102
89
103
90
## State Reconciler
104
-
State reconcilers define how incoming persisted state is merged in with existing default state. It is critical to choose the right state reconciler for your state shape. There are three options that ship out of the box, lets look at how each operates:
91
+
State reconcilers define how incoming state is merged in with initial state. It is critical to choose the right state reconciler for your state. There are three options that ship out of the box, lets look at how each operates:
105
92
106
-
1. hardSet (`import hardSet from 'redux-persist/lib/stateReconciler/hardSet'`)
93
+
1.**hardSet** (`import hardSet from 'redux-persist/lib/stateReconciler/hardSet'`)
107
94
This will hard set incoming state. This can be desirable in some cases where persistReducer is nested deeper in your reducer tree, or if you do not rely on initialState in your reducer.
108
-
-**INCOMING STATE**: `{ foo: incomingFoo }`
109
-
-**INITIAL STATE**: `{ foo: initialFoo, bar: initialBar }`
110
-
-**RECONCILED STATE**: `{ foo: incomingFoo }` // note bar has been dropped
111
-
2. autoMergeLevel1 (default)
95
+
-**incoming state**: `{ foo: incomingFoo }`
96
+
-**initial state**: `{ foo: initialFoo, bar: initialBar }`
97
+
-**reconciled state**: `{ foo: incomingFoo }` // note bar has been dropped
98
+
2.**autoMergeLevel1** (default)
112
99
This will auto merge one level deep. Auto merge means if the some piece of substate was modified by your reducer during the REHYDRATE action, it will skip this piece of state. Level 1 means it will shallow merge 1 level deep.
113
-
-**INCOMING STATE**: `{ foo: incomingFoo }`
114
-
-**INITIAL STATE**: `{ foo: initialFoo, bar: initialBar }`
115
-
-**RECONCILED STATE**: `{ foo: incomingFoo, bar: initialBar }`
116
-
3. autoMergeLevel2
100
+
-**incoming state**: `{ foo: incomingFoo }`
101
+
-**initial state**: `{ foo: initialFoo, bar: initialBar }`
3.**autoMergeLevel2** (`import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2'`)
117
104
This acts just like autoMergeLevel1, except it shallow merges two levels
118
-
-**INCOMING STATE**: `{ foo: incomingFoo }`
119
-
-**INITIAL STATE**: `{ foo: initialFoo, bar: initialBar }`
120
-
-**RECONCILED STATE**: `{ foo: mergedFoo, bar: initialBar }`
105
+
-**incoming state**: `{ foo: incomingFoo }`
106
+
-**initial state**: `{ foo: initialFoo, bar: initialBar }`
107
+
-**reconciled state**: `{ foo: mergedFoo, bar: initialBar }` // note: initialFoo and incomingFoo are shallow merged
121
108
122
109
#### Example
123
110
```js
@@ -149,7 +136,7 @@ const persistConfig = {
149
136
```
150
137
151
138
## Nested Persists
152
-
Nested persist can be useful for a variety of reasons including different storage adapters, code splitting, or deep filtering. For example blacklist and whitelist only work one level deep, but we can use a nested persist to blacklist a deep value:
139
+
Nested persist can be useful for including different storage adapters, code splitting, or deep filtering. For example while blacklist and whitelist only work one level deep, but we can use a nested persist to blacklist a deeper value:
Redux Persist ships with `createMigrate`, which helps create a synchronous migration for moving from any version of stored state to the current state version. [[Additional information]](./docs/migrations.md)
184
171
185
172
## Transforms
186
-
187
173
Transforms allow you to customize the state object that gets persisted and rehydrated.
188
174
189
175
There are several libraries that tackle some of the common implementations for transforms.
The createTransform function takes three parameters.
219
-
1.A function that gets called right before state is persisted.
220
-
2.A function that gets called right before state is rehydrated.
221
-
3.A config object.
205
+
1.An "inbound" function that gets called right before state is persisted.
206
+
2.An "outbound" function that gets called right before state is rehydrated.
207
+
3.Config.
222
208
223
209
## Storage Engines
224
210
-**localStorage**`import storage from 'redux-persist/lib/storage'`
225
-
-**sessionStorage**`import sessionStorage from 'redux-persist/lib/storage/session'`
226
-
-**AsyncStorage** react-native `import storage from 'redux-persist/lib/storage'`
211
+
-**sessionStorage**`import storageSession from 'redux-persist/lib/storage/session'`
212
+
-**AsyncStorage** react-native `import { AsyncStorage } from 'react-native'`
227
213
-**[localForage](https://github.com/mozilla/localForage)** recommended for web
228
214
-**[electron storage](https://github.com/psperber/redux-persist-electron-storage)** Electron support via [electron store](https://github.com/sindresorhus/electron-store)
229
215
-**[redux-persist-filesystem-storage](https://github.com/robwalkerco/redux-persist-filesystem-storage)** react-native, to mitigate storage size limitations in android ([#199](https://github.com/rt2zz/redux-persist/issues/199), [#284](https://github.com/rt2zz/redux-persist/issues/284))
0 commit comments