<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Harshit Bhawsar </title>
    <description>The latest articles on Forem by Harshit Bhawsar  (@harshitscript).</description>
    <link>https://forem.com/harshitscript</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1940463%2F7b9e7026-a779-449f-bcdc-4398d96db343.jpeg</url>
      <title>Forem: Harshit Bhawsar </title>
      <link>https://forem.com/harshitscript</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/harshitscript"/>
    <language>en</language>
    <item>
      <title>Persist Redux State</title>
      <dc:creator>Harshit Bhawsar </dc:creator>
      <pubDate>Tue, 20 Aug 2024 04:43:54 +0000</pubDate>
      <link>https://forem.com/harshitscript/persist-redux-state-3o3j</link>
      <guid>https://forem.com/harshitscript/persist-redux-state-3o3j</guid>
      <description>&lt;h2&gt;
  
  
  What does Persist Redux state mean?
&lt;/h2&gt;

&lt;p&gt;One common challenge in React applications is rehydrating the Redux state after a page reload or between user sessions. A typical workaround is to re-fetch the data via an API call and store it in the Redux state. However, you can now rehydrate the Redux state without additional API calls using a technique called Persisted Redux State.&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;redux-persist&lt;/code&gt; package
&lt;/h2&gt;

&lt;p&gt;This package and the typical redux packages &lt;code&gt;@reduxjs/toolkit&lt;/code&gt; and &lt;code&gt;react-redux&lt;/code&gt; can be used to create a redux state that can persist across the page reload or user session in the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You have a running react application.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installations
&lt;/h2&gt;

&lt;p&gt;Use this command to install all the necessary packages to set up a persisting redux state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i --save @reduxjs/toolkit react-redux redux-persist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setting up a Persisting Redux State
&lt;/h2&gt;

&lt;p&gt;1.Configure your redux store [store.js].&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { combineReducers, configureStore } from "@reduxjs/toolkit";
import sampleSlice from "./sampleSlice";
import storageSession from "redux-persist/lib/storage/session"; // session storage
import { FLUSH, PAUSE, PERSIST, persistReducer, persistStore, PURGE, REGISTER, REHYDRATE } from 'redux-persist';

const rootReducer = combineReducers({
     sample : sampleSlice.reducer;
})
const persistConfig = {
     key:'root',
     storage: storageSession,
}
const persistedReducer = persistReducer(persistConfig, rootReducer)
const store = configureStore({
     reducer: persistedReducer,
     middleware: (getDefaultMiddleware) =&amp;gt; 
               getDefaultMiddleware({
                   serializableCheck: {
                         ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
           },
     }) 
})
const persister = persistStore(store);
export default store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: If you want your redux to persist not only between the reloads but also between the user-sessions in the browser replace the&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import storageSession from "redux-persist/lib/storage/session"; // session storage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 import with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import storageSession from "redux-persist/lib/storage"; // local storage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Wrap your Root Component [index.js].&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { BrowserRouter } from 'react-router-dom';
import store, { persistor } from './store';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(&amp;lt;BrowserRouter&amp;gt;
    &amp;lt;Provider store={store}&amp;gt;
        &amp;lt;PersistGate loading={null} persistor={persistor}&amp;gt;
            &amp;lt;App /&amp;gt;
        &amp;lt;/PersistGate&amp;gt;
    &amp;lt;/Provider&amp;gt;
&amp;lt;/BrowserRouter&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and done! your persisting redux state is ready.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>redux</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
