Skip to content

Commit 5a29b0a

Browse files
committed
persist setup
1 parent 74463aa commit 5a29b0a

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import ReactDOM from 'react-dom/client';
33
import App from './components/App';
44
import './index.css';
55
import { Provider as ReduxProvider } from 'react-redux';
6-
import persistor from './redux/store.js';
6+
import { PersistGate } from 'redux-persist/integration/react';
7+
import { persistor, store } from './redux/store';
78

89
ReactDOM.createRoot(document.getElementById('root')).render(
9-
<ReduxProvider store={persistor}>
10-
<App />
10+
<ReduxProvider store={store}>
11+
<PersistGate loading={null} persistor={persistor}>
12+
<App />
13+
</PersistGate>
1114
</ReduxProvider>
1215
);

src/redux/store.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { configureStore } from '@reduxjs/toolkit';
1+
import { combineReducers, configureStore } from '@reduxjs/toolkit';
22
import { contactsReducer } from './slices/contactsSlice';
33
import { filterReducer } from './slices/filterSlice';
44

5+
import { persistReducer, persistStore } from 'redux-persist';
6+
import storage from 'redux-persist/lib/storage';
7+
58
/**
69
* In store, pentru fiecare "particica" din state-ul aplicatiei, o sa asignam un reducer care se va ocupa exclusiv de logica pentru acea "particica".
710
*
@@ -12,11 +15,25 @@ import { filterReducer } from './slices/filterSlice';
1215
* }
1316
*/
1417

15-
const store = configureStore({
16-
reducer: {
18+
const persistConfig = {
19+
key: 'root',
20+
storage,
21+
};
22+
23+
const persistedReducer = persistReducer(
24+
persistConfig,
25+
combineReducers({
1726
contacts: contactsReducer,
1827
filter: filterReducer,
19-
},
28+
})
29+
);
30+
31+
export const store = configureStore({
32+
reducer: persistedReducer,
33+
middleware: getDefaultMiddleware =>
34+
getDefaultMiddleware({
35+
serializableCheck: false,
36+
}),
2037
});
2138

22-
export default store;
39+
export const persistor = persistStore(store);

0 commit comments

Comments
 (0)