Skip to content

Commit

Permalink
persist card to localStorage with redux-persist
Browse files Browse the repository at this point in the history
  • Loading branch information
jez321 committed Nov 10, 2019
1 parent 709cdb9 commit 064596c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"react-scripts": "3.1.2",
"redux": "^4.0.4",
"redux-logger": "^3.0.6",
"redux-persist": "^6.0.0",
"reselect": "^4.0.0"
},
"scripts": {
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { BrowserRouter } from 'react-router-dom';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import store from './redux/store';
import { persistor, store } from './redux/store';
import { PersistGate } from 'redux-persist/integration/react';

ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<App />
<PersistGate persistor={persistor}>
<App />
</PersistGate>
</BrowserRouter>
</Provider>
, document.getElementById('root')
Expand Down
12 changes: 11 additions & 1 deletion src/redux/root-reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { combineReducers } from 'redux';
import userReducer from './user/user-reducer';
import cartReducer from './cart/cart-reducer';
import { persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';

export default combineReducers({
const persistconfig = {
key: 'root',
storage,
whitelist: ['cart']
}

const rootReducer = combineReducers({
user: userReducer,
cart: cartReducer
});

export default persistReducer(persistconfig, rootReducer);
6 changes: 3 additions & 3 deletions src/redux/store.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createStore, applyMiddleware } from 'redux';
import logger from 'redux-logger';
import rootReducer from './root-reducer';
import { persistStore } from 'redux-persist';

const middleware = [logger];

const store = createStore(rootReducer, applyMiddleware(...middleware));

export default store;
export const store = createStore(rootReducer, applyMiddleware(...middleware));
export const persistor = persistStore(store);

0 comments on commit 064596c

Please sign in to comment.