Home
Daily
📦 Redux Toolkit: Persisting Redux State with `redux-persist`
November 10, 2025
1 min

Table Of Contents

01
💾 Why Persist State?
02
📦 Step 1 — Install redux-persist
03
🧱 Step 2 — Configure the Persisted Store
04
🔗 Step 3 — Wrap App with PersistGate
05
🧠 Avoiding Common Pitfalls
06
✅ Result
07
🎯 Summary

Keeping state even after the user refreshes the page.

By default, Redux resets state when the page reloads. That means any stored data—like cart items, theme preferences, or user selections—disappears. To fix this, we can persist the Redux state to localStorage.

In this lesson, we’ll learn:

  • How to use redux-persist
  • How to choose which parts of Redux state to persist
  • Common mistakes and how to avoid them

💾 Why Persist State?

Imagine a shopping cart app. A user adds 3 items → refreshes the page → cart becomes empty.

That’s a bad user experience.

Persisting state ensures: ✅ Data doesn’t disappear ✅ User experience feels smooth ✅ App behaves more like a native app


📦 Step 1 — Install redux-persist

Run:

npm install redux-persist

🧱 Step 2 — Configure the Persisted Store

Open your store.js:

import { configureStore } from "@reduxjs/toolkit";
import storage from "redux-persist/lib/storage";
import { persistReducer, persistStore } from "redux-persist";
import { combineReducers } from "redux";
import thunk from "redux-thunk";
import counterReducer from "./features/counter/counterSlice";
import pokemonReducer from "./features/pokemon/pokemonSlice";
const persistConfig = {
key: "root",
storage,
whitelist: ["counter"], // choose which slices to persist
};
const rootReducer = combineReducers({
counter: counterReducer,
pokemon: pokemonReducer,
});
const persistedReducer = persistReducer(persistConfig, rootReducer);
export const store = configureStore({
reducer: persistedReducer,
middleware: [thunk],
});
export const persistor = persistStore(store);

Why whitelist?

We don’t always want to persist everything.

Examples:

SliceShould Persist?Why
User theme (light/dark)✅ YesGood user UX
Login token❗ MaybeOnly if securely handled
API data lists❌ NoCan be re-fetched easily
Shopping cart✅ YesCritical user context

Only persist what matters.


🔗 Step 3 — Wrap App with PersistGate

Open main.jsx / index.js:

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { Provider } from "react-redux";
import { store, persistor } from "./store";
import { PersistGate } from "redux-persist/integration/react";
ReactDOM.createRoot(document.getElementById("root")).render(
<Provider store={store}>
<PersistGate loading={<p>Loading...</p>} persistor={persistor}>
<App />
</PersistGate>
</Provider>
);

Now Redux will restore state from localStorage before rendering the UI.


🧠 Avoiding Common Pitfalls

ProblemCauseFix
“Could not serialize state” errorsNon-serializable values in stateStore only plain objects & primitives
Persisting too much slows the appPersisting large API listsUse whitelist to persist only important state
UI shows outdated dataAPI data persisted accidentallyDon’t persist API slices

Golden Rule:

Persist state that is meaningful to the user, not just convenient for the developer.


✅ Result

You now have:

  • Redux state saved across refresh
  • Clean store configuration
  • Control over which state persists

Your app is now more user-friendly and feels professional and polished.


🎯 Summary

ConceptPurpose
redux-persistSaves Redux state to storage
whitelistControls which slices get persisted
PersistGateDelays UI until state is restored

State persistence is a powerful UX improvement—use it wisely.



Tags

#redux

Share

Related Posts

Redux Toolkit
🚀 CI/CD: The Engine Behind Modern Software Delivery
December 13, 2025
2 min
© 2025, All Rights Reserved.
Powered By

Social Media

githublinkedinyoutube