Skip to content

Commit 93e4242

Browse files
Added more comments to understand more
1 parent a291ddd commit 93e4242

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ What should be the structure of your redux store?
1818

1919
```javascript
2020
// ./store/store
21-
2221
import { createStore, applyMiddleware, combineReducers } from "redux";
2322
import { createWrapper, HYDRATE } from "next-redux-wrapper";
2423
import thunkMiddleware from "redux-thunk";
25-
import counter from "../store/counter/reducer";
24+
import counter from "./counter/reducer";
2625

2726
//COMBINING ALL REDUCERS
2827
const combinedReducer = combineReducers({
@@ -41,30 +40,33 @@ const bindMiddleware = (middleware) => {
4140

4241
const makeStore = ({ isServer }) => {
4342
if (isServer) {
44-
//If it's on server side, create a store simply
43+
//If it's on server side, create a store
4544
return createStore(combinedReducer, bindMiddleware([thunkMiddleware]));
4645
} else {
47-
//If it's on client side, create a store with a persistability feature
46+
//If it's on client side, create a store which will persist
4847
const { persistStore, persistReducer } = require("redux-persist");
4948
const storage = require("redux-persist/lib/storage").default;
5049

5150
const persistConfig = {
5251
key: "nextjs",
53-
whitelist: ["counter"], // make sure it does not clash with server keys
54-
storage,
52+
whitelist: ["counter"], // only counter will be persisted, add other reducers if needed
53+
storage, // if needed, use a safer storage
5554
};
5655

57-
const persistedReducer = persistReducer(persistConfig, combinedReducer);
56+
const persistedReducer = persistReducer(persistConfig, combinedReducer); // Create a new reducer with our existing reducer
57+
5858
const store = createStore(
5959
persistedReducer,
6060
bindMiddleware([thunkMiddleware])
61-
);
62-
store.__persistor = persistStore(store);
61+
); // Creating the store again
62+
63+
store.__persistor = persistStore(store); // This creates a persistor object & push that persisted object to .__persistor, so that we can avail the persistability feature
64+
6365
return store;
6466
}
6567
};
6668

67-
// export an assembled wrapper
69+
// Export the wrapper & wrap the pages/_app.js with this wrapper only
6870
export const wrapper = createWrapper(makeStore);
6971
```
7072

store/store.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,22 @@ const makeStore = ({ isServer }) => {
2929

3030
const persistConfig = {
3131
key: "nextjs",
32-
whitelist: ["counter"], // make sure it does not clash with server keys
33-
storage, // use a safer storage
32+
whitelist: ["counter"], // only counter will be persisted, add other reducers if needed
33+
storage, // if needed, use a safer storage
3434
};
3535

36-
const persistedReducer = persistReducer(persistConfig, combinedReducer);
36+
const persistedReducer = persistReducer(persistConfig, combinedReducer); // Create a new reducer with our existing reducer
37+
3738
const store = createStore(
3839
persistedReducer,
3940
bindMiddleware([thunkMiddleware])
40-
);
41-
store.__persistor = persistStore(store);
41+
); // Creating the store again
42+
43+
store.__persistor = persistStore(store); // This creates a persistor object & push that persisted object to .__persistor, so that we can avail the persistability feature
44+
4245
return store;
4346
}
4447
};
4548

46-
// export an assembled wrapper
49+
// Export the wrapper & wrap the pages/_app.js with this wrapper only
4750
export const wrapper = createWrapper(makeStore);

0 commit comments

Comments
 (0)