Skip to content

Commit

Permalink
Merge pull request #3 from iVladyuser/redux_Persist
Browse files Browse the repository at this point in the history
added persist
  • Loading branch information
iVladyuser authored Nov 16, 2023
2 parents b634718 + 64b3a89 commit 76fdd0b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
15 changes: 15 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 @@ -12,6 +12,7 @@
"react-dom": "^18.1.0",
"react-redux": "^8.1.3",
"react-scripts": "5.0.1",
"redux-persist": "^6.0.0",
"styled-components": "^6.1.1",
"web-vitals": "^2.1.3"
},
Expand Down
5 changes: 0 additions & 5 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useEffect } from 'react';
import { nanoid } from 'nanoid';

import { ContactForm, ContactList, Filter } from 'components';
Expand All @@ -12,10 +11,6 @@ const App = () => {

const filter = useSelector(state => state.filterStore);

useEffect(() => {
localStorage.setItem('contacts', JSON.stringify(contacts));
}, [contacts]);

const handleDeleteContact = contactId => {
dispatch(deleteContact(contactId));
};
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import { Provider } from 'react-redux';
import { store } from './redux/store';
import { store, persistor } from './redux/store';
import { PersistGate } from 'redux-persist/integration/react';

import App from 'components/App';
import './index.css';


ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<Provider store={store}>
<App />
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>
</React.StrictMode>
);
31 changes: 29 additions & 2 deletions src/redux/store.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
import { configureStore } from '@reduxjs/toolkit';
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
} from 'redux-persist';
import storage from 'redux-persist/lib/storage';

import { contactsReducer } from './contacts/phoneBookSlice';
import {filterReducer} from './contacts/filterSlice'
import { filterReducer } from './contacts/filterSlice';

const contactsConfig = {
key: 'contacts',
storage,
// whitelist: ['contacts'],
// or blacklist
};

export const store = configureStore({
reducer: {
contactsStore: contactsReducer,
contactsStore: persistReducer(contactsConfig, contactsReducer),
filterStore: filterReducer,
},
middleware: getDefaultMiddleware =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
});

export const persistor = persistStore(store);

0 comments on commit 76fdd0b

Please sign in to comment.