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
There are a couple of things you need to get started. Firstly add the `persistMiddleware` to your redux middleware.
18
+
There are a couple of things you need to get started. Firstly add the return of `createPersistMiddleware` to your redux middleware.
19
19
20
-
The middleware parameters are a save and load method, you don't have to write these yourself, they are [available as separate packages below](#providers).
20
+
The `createPersistMiddleware()` parameters are:
21
+
- the structure of your store and use arrays to define what values you want the middleware to persist
22
+
- a save method
23
+
- a load method
24
+
- (optional) whether to enable debug or not
25
+
26
+
The structure of your store consist of an object that takes some objects, and each object has to follow the following schema:
27
+
28
+
- The key of each value in the persist object should match your store shape, a nested reducer would be defined as such: `data.device`.
29
+
- In the `values` key, you can customize which values the package will keep track of. If nothing is provided to the `values` field, all fields will be saved.
30
+
- A `key` is required to be defined for each persisted reducer, this will be used as the async storage key, it keeps a static map of your data which is independent of its shape, which is useful as your application grows.
31
+
- An optional key of `action` can be defined to explicitly declare which action type should trigger a load of that reducer, without this value each load type is generated automatically from the state shape. e.g. to load `"data.device"` fire `LOAD_DATA_DEVICE`, to load `"subscriptionOrders"` fire `LOAD_SUBSCRIPTION_ORDERS`.
32
+
- An optional `automatic` key can also be provided to specify if that reducer should be loaded automatically, without having to dispatch the action. It defaults to `true`.
33
+
34
+
For the load and save method, you don't have to write them by yourself, they are [available as separate packages below](#providers).
21
35
22
36
```js
23
37
// e.g. save and load methods -> available as separate packages below
24
38
constsaveState= (key, state) =>...
25
39
constloadState= (key) =>...
26
40
27
-
// store.js
28
-
29
-
constmiddleware= [
30
-
() =>persistMiddleware(saveState, loadState)
31
-
];
32
-
33
-
exportconststore=createStore(
34
-
reducers,
35
-
applyMiddleware(...middleware)
36
-
);
37
-
```
38
-
39
-
The next step is to define the structure of your store and use arrays to define what values you want the middleware to persist.
40
-
41
-
```js
42
-
constpersistThisData= {
41
+
/**
42
+
You define the structure that you want to save
43
+
in this object, and then pass it as an argument.
44
+
Instructions on how to create this object are above.
The `createPersistMachine` takes three arguments: the persist object defined above, the redux store, and an optional bool value to enable debugging (default : false).
85
+
Your reducer data will automatically saved when the values are changed. You can load each reducer using its load action (to see all the load actions generated in your console set the fourth parameter of `createPersistMachine` to `true`).
74
86
75
-
- The key of each value in the persist object should match your store shape, a nested reducer would be defined as such: "data.device". If nothinof provided to the `values` field, all fields will be saved.
76
-
- A `key` is required to be defined for each persisted reducer, this will be used as the async storage key, it keeps a static map of your data which is independent of its shape, which is useful as your application grows.
77
-
- An optional key of `action` can be defined to explicitly declare which action type should trigger a load of that reducer, without this value each load type is generated automatically from the state shape. e.g. to load `"data.device"` fire `LOAD_DATA_DEVICE`, to load `"subscriptionOrders"` fire `LOAD_SUBSCRIPTION_ORDERS`.
78
-
- An optional `automatic` key can also be provided to specify if that reducer should be loaded automatically, without having to dispatch the action. It defaults to `true`.
87
+
After setting the middleware in the store, you need to call `createPersistMachine.run` and pass the store as an argument.
79
88
80
-
Your reducer data will automatically saved when the values are changed. You can load each reducer using its load action (to see all the load actions generated in your console set the third parameter of `createPersistMachine` to `true`).
89
+
```js
90
+
createPersistMachine.run(store)
91
+
```
81
92
82
93
### Loading Data
83
94
@@ -94,7 +105,7 @@ case LOAD_SUBSCRIPTION_ORDERS: {
94
105
95
106
This allows you to have loading on a per reducer basis separated across the application for stored data instead of having the full application wait for the data to be loaded.
96
107
97
-
The middleware runs: `action.payload = { ...storedData, ...action.payload };` to add the saved data to the payload when the LOAD_ACTION is triggered. You can also pass additional data in your payload to add context to your `LOAD_ACTIONS` for complex conditional consummation of the loaded data in your reducers.
108
+
The middleware runs: `action.payload = { ...storedData, ...action.payload };` to add the saved data to the payload when the `LOAD_ACTION` is triggered. You can also pass additional data in your payload to add context to your `LOAD_ACTIONS` for complex conditional consummation of the loaded data in your reducers.
0 commit comments