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

Can't make it to work with react-redux-toolkit #33

Open
springerjanek opened this issue Sep 27, 2022 · 3 comments
Open

Can't make it to work with react-redux-toolkit #33

springerjanek opened this issue Sep 27, 2022 · 3 comments

Comments

@springerjanek
Copy link

import { configureStore } from "@reduxjs/toolkit";
import authReducer from "./authSlice";
import storage from "redux-persist/lib/storage";
import { persistReducer, persistStore } from "redux-persist";
import expireReducer from "redux-persist-expire";
import thunk from "redux-thunk";

const persistConfig = {
  key: "root",
  storage,
  blacklist: ["error", "success"],
  transforms: [
    expireReducer("auth", {
      expireSeconds: 5,
      expiredState: {
        isLoggedIn: "false",
        id: "",
      },
    }),
  ],
};

const persistedReducer = persistReducer(persistConfig, authReducer);

export const store = configureStore({
  reducer: {
    auth: persistedReducer,
  },
  middleware: [thunk],
});

export const persistor = persistStore(store);

The auth should be resetted in 5 seconds and it doesn't happen.

@m5ultra
Copy link

m5ultra commented Mar 9, 2023

import { configureStore } from '@reduxjs/toolkit'
const createDebugger = require('redux-flipper').default
import AsyncStorage from '@react-native-async-storage/async-storage'
// import AsyncStorage from '@react-native-community/async-storage'
import { persistReducer, persistStore } from 'redux-persist'

import home from '@/store/models/home.slice'
import category from '@/store/models/category.slice'
import expireReducer from 'redux-persist-expire'

const persistConfig = {
key: 'root',
version: 1,
storage: AsyncStorage,
whitelist: ['myCategory'],
// 如 ms(毫秒)、s(秒)、m(分钟)、h(小时)和 d(天)
// expireTime: 1000 * 5,
transforms: [
expireReducer('category', {
expireSeconds: 120,
expiredState: {
myCategory: [],
},
}),
],
}

const store = configureStore({
reducer: {
home,
...{
category: persistReducer(persistConfig, category),
},
},
middleware: (getDefaultMiddleware) =>
DEV
? getDefaultMiddleware({ serializableCheck: false }).concat(
createDebugger()
)
: getDefaultMiddleware({
serializableCheck: false,
}),
})
export const persistor = persistStore(store)

export type RootState = ReturnType
export type RootDispatch = typeof store.dispatch

export default store

@m5ultra
Copy link

m5ultra commented Mar 9, 2023

NOT WORK.

@appkery
Copy link

appkery commented May 16, 2023

It works very well with redux toolkit.
Here is the code sample which works.

type AuthState = {
  auth: {
    id: string | null
  }
}

const persistConfig = {
  key: 'root',
  version: 1,
  storage,
  transforms: [
    expireReducer('auth', {
      expireSeconds: 60,
      autoExpire: true,
    }),
  ],
}

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

3 participants