RRR uzumaki
RRR uzumaki

Reputation: 1328

using redux-persist with redux thunk

Hey guys i was using redux thunk with nextjs and now i want to add redux-persist in my app. So initially my code is like

import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import reducer from './reducers';

export const makeStore = (initialState, options) => {
    return createStore(reducer, initialState, composeWithDevTools(applyMiddleware(thunk)));
};

can anyone help me with only in the setup with redux persist now ? I have tried some solution but didnt worked out

Upvotes: 2

Views: 6802

Answers (2)

Dhara Charola
Dhara Charola

Reputation: 352

Here is the link of the article having simple but detailed steps to integrate & use the package in your existing app.

Usually, I follow the same structure that you have for the redux store. This integration always works for me. So I hope this will help you out too.

Upvotes: 2

Muhammad Emad Al-Din
Muhammad Emad Al-Din

Reputation: 371

If you really need to persist your redux state there are two options as far as I know: first you could use react-persist as per your wish like this

import { createStore, applyMiddleware } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import reducer from './reducers';
import storage from 'redux-persist/lib/storage';

const persistConfig = {
    key: 'reducer',
    storage: storage,
    whitelist: ['reducer'] // or blacklist to exclude specific reducers
 };
const presistedReducer = persistReducer(persistConfig, reducer );
const store = createStore(presistedReducer, 
composeWithDevTools(applyMiddleware(thunk)));
const persistor = persistStore(store);
export { persistor, store };

and then in your component you do the following as instructed in their documentation

import { PersistGate } from 'redux-persist/integration/react';

// ... normal setup, create store and persistor, import components etc.

const App = () => {
return (
   <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <RootComponent />
      </PersistGate>
   </Provider>
  );
};

or you could simply do the following without relying on a library

import {
  createStore, combineReducers, compose, applyMiddleware,
 } from 'redux';
import thunk from 'redux-thunk';
import reducer from './reducers';
function saveToLocalStorage(state) {
    const serializedState = JSON.stringify(state);
    localStorage.setItem('state', serializedState);
}

function loadFromLocalStorage() {
const serializedState = localStorage.getItem('state');
if (serializedState === null) return undefined;
   return JSON.parse(serializedState);
}

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const presistedState = loadFromLocalStorage();
const store = createStore(
    reducer,
    presistedState,
    composeEnhancers(applyMiddleware(thunk)),
 );
store.subscribe(() => saveToLocalStorage(store.getState()));
export default store;

Upvotes: 16

Related Questions