Browse Source

Ajout de redux-persist: persistance du store

Lou 3 years ago
parent
commit
8f660f3fe4
4 changed files with 34 additions and 7 deletions
  1. 1
    0
      package.json
  2. 5
    2
      src/main.jsx
  3. 23
    5
      src/redux/store/index.js
  4. 5
    0
      yarn.lock

+ 1
- 0
package.json View File

@@ -14,6 +14,7 @@
14 14
     "react-redux": "^7.2.4",
15 15
     "react-router-dom": "^5.2.0",
16 16
     "redux": "^4.1.0",
17
+    "redux-persist": "^6.0.0",
17 18
     "redux-thunk": "^2.3.0"
18 19
   },
19 20
   "devDependencies": {

+ 5
- 2
src/main.jsx View File

@@ -1,7 +1,8 @@
1 1
 import React from "react";
2 2
 import ReactDOM from "react-dom";
3
-import store from "./redux/store/index";
4 3
 import { Provider } from "react-redux";
4
+import { PersistGate } from "redux-persist/integration/react";
5
+import { store, persistor } from "./redux/store/index";
5 6
 
6 7
 import App from "./App.jsx";
7 8
 import "./main.scss";
@@ -9,7 +10,9 @@ import "./main.scss";
9 10
 ReactDOM.render(
10 11
 	<React.StrictMode>
11 12
 		<Provider store={store}>
12
-			<App />
13
+			<PersistGate loading={null} persistor={persistor}>
14
+				<App />
15
+			</PersistGate>
13 16
 		</Provider>
14 17
 	</React.StrictMode>,
15 18
 	document.getElementById("root")

+ 23
- 5
src/redux/store/index.js View File

@@ -1,12 +1,30 @@
1
-import reducer from "../reducer/index"
2 1
 import { createStore, compose, applyMiddleware } from 'redux';
3 2
 import thunk from 'redux-thunk';
3
+import { persistStore, persistReducer } from 'redux-persist'
4
+import storage from 'redux-persist/lib/storage'
5
+import reducer from "../reducer/index"
4 6
 
5 7
 const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
6 8
 
9
+const persistConfig = {
10
+  key: 'root',
11
+  storage,
12
+}
13
+
14
+const persistedReducer = persistReducer(persistConfig, reducer)
15
+
16
+// const store = createStore(
17
+//   reducer,
18
+//   composeEnhancer(applyMiddleware(thunk))
19
+// );
20
+
21
+// export default store;
22
+
7 23
 const store = createStore(
8
-  reducer,
9
-  composeEnhancer(applyMiddleware(thunk)),
10
-);
24
+  persistedReducer,
25
+  composeEnhancer(applyMiddleware(thunk))
26
+  );
27
+
28
+const persistor = persistStore(store);
11 29
 
12
-export default store;
30
+export { store, persistor };

+ 5
- 0
yarn.lock View File

@@ -692,6 +692,11 @@ readdirp@~3.6.0:
692 692
   dependencies:
693 693
     picomatch "^2.2.1"
694 694
 
695
+redux-persist@^6.0.0:
696
+  version "6.0.0"
697
+  resolved "https://registry.yarnpkg.com/redux-persist/-/redux-persist-6.0.0.tgz#b4d2972f9859597c130d40d4b146fecdab51b3a8"
698
+  integrity sha512-71LLMbUq2r02ng2We9S215LtPu3fY0KgaGE0k8WRgl6RkqxtGfl7HUozz1Dftwsb0D/5mZ8dwAaPbtnzfvbEwQ==
699
+
695 700
 redux-thunk@^2.3.0:
696 701
   version "2.3.0"
697 702
   resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"

Loading…
Cancel
Save