Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failed to preserved initial state value at rehydrate #1451

Open
vishal590 opened this issue Sep 7, 2023 · 0 comments
Open

failed to preserved initial state value at rehydrate #1451

vishal590 opened this issue Sep 7, 2023 · 0 comments

Comments

@vishal590
Copy link

vishal590 commented Sep 7, 2023

got 10000000 at init and at persist but at rehydrate got 0

import { createSlice } from "@reduxjs/toolkit";

const initialState = {
  name: "",
  address: "",
  balance: 10000000,
  isLogin: localStorage.getItem("token") ? true : false,
};

export const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {
    setUser: (state, action) => {
      return {
        ...state,
        ...action.payload,
      };
    },
    updateBalance: (state, action) => {
      return {
        ...state,
        balance: action.payload,
      };
    },
    updateAddress: (state, action) => {
      return {
        ...state,
        address: action.payload,
      };
    },
    updateName: (state, action) => {
      return {
        ...state,
        name: action.payload,
      };
    },
    logIn: (state, action) => {
      localStorage.setItem("token", action.payload);
      return {
        ...state,
        isLogin: true,
      };
    },
    logOut: (state) => {
      state.name = "";
      state.address = "";
      state.balance = 0;
      state.isLogin = false;
      localStorage.removeItem("token");
    },
  },
});


export const { setUser, updateName, updateBalance, updateAddress, logIn, logOut } =
  userSlice.actions;
export const getUser = (state) => state.user;
export default userSlice.reducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant