2023-12-07
3631
#react#redux
Taminoturoko Briggs
113568
Dec 7, 2023 ⋅ 12 min read

Persist state with Redux Persist using Redux Toolkit in React

Taminoturoko Briggs Software developer and technical writer. Core languages include JavaScript and Python.

Recent posts:

Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries — Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 2024 ⋅ 10 min read
Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Django’s server-side functionalities and React’s interactive UI.

Kayode Adeniyi
Apr 18, 2024 ⋅ 7 min read
Using Aoi Js To Build A Bot For Discord

Using aoi.js to build a bot on Discord

Explore how the aoi.js library makes it easy to create Discord bots with useful functionalities for frontend applications.

Rahul Padalkar
Apr 17, 2024 ⋅ 9 min read
Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 ⋅ 11 min read
View all posts

7 Replies to "Persist state with Redux Persist using Redux Toolkit in React"

  1. I’m amazed that a developer suggests to use a package that hasn’t been updated for 3 years. Redux, React has moved on – many people that tried this package failed for that reason. But even if that wouldn’t already be the case, do you really want to recommend to developers a strategy that is doomed sooner or later?

  2. the top code has a lot of issue:
    by writing bottom code you can implement currectly:

    my store folder structure fro react redux:

    store __ features __ user.ts
    | | __ auth.ts
    | __ reducer.ts
    | __ store.ts

    ————————————————————————————–

    // features folder => user.ts

    import { createSlice } from “@reduxjs/toolkit”;
    import storage from “redux-persist/lib/storage”;
    import { persistReducer } from “redux-persist”;

    export const userSlice = createSlice({
    name: “user”,
    initialState: {
    fullname: “jack abc”,
    age: 23,
    gender: “male”,
    },
    reducers: {
    newUser: (state, { payload }) => {
    state.fullname = payload.fullname;
    state.age = payload.age;
    state.gender = payload.gender;
    },
    removeUser(state) {
    state.fullname = “”;
    state.age = 0;
    state.gender = “”;
    },
    },
    });

    const persistConfig = {
    key: “user”,
    storage,
    version: 1,
    };

    export const userReducer = persistReducer(persistConfig, userSlice.reducer);

    export default userSlice;

    ————————————————————————————–

    // reducer file (store root directory) => reducer.ts

    import { combineReducers } from “@reduxjs/toolkit”;

    import { userReducer } from “./features/user”;
    import { authReducer } from “./features/auth”;

    const rootReducer = combineReducers({
    auth: authReducer,
    user: userReducer,
    });

    //? by bottom code you can persist all reducers
    // import storage from “redux-persist/lib/storage”;
    // import { persistReducer } from “redux-persist”;
    // const persistConfig = {
    // key: “all”,
    // storage,
    // version: 1,
    // };
    // export default persistReducer(persistConfig, rootReducer);

    export default rootReducer;

    ————————————————————————————–

    // store file (store root directory) => store.ts

    import { configureStore } from “@reduxjs/toolkit”;
    import { persistStore } from “redux-persist”;

    import rootReducer from “./reducer”;

    // Create the store
    const store = configureStore({
    reducer: rootReducer,
    middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
    serializableCheck: false,
    }),
    });

    // Persist the store for redux persist
    export const persistor = persistStore(store);

    // Get the State type
    export type RootState = ReturnType;
    export type AppDispatch = typeof store.dispatch;

    export default store;

Leave a Reply