Need help how to use redux-persist in new typescript template #4298
Unanswered
PANKAJBAJAJ045
asked this question in
Q&A
Replies: 1 comment 1 reply
-
what is the error you're getting? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I created a new project using this URL "npx degit reduxjs/redux-templates/packages/vite-template-redux my-app"
When I try to use redux-persist, I get an error, In documentation normal store is used but in the template below code is used to create a store.
// Infer the
RootState
type from the root reducerexport type RootState = ReturnType;
// The store setup is wrapped in
makeStore
to allow reuse// when setting up tests that need the same store config
export const makeStore = (preloadedState?: Partial) => {
const store = configureStore({
reducer: rootReducer,
preloadedState
})
// configure listeners using the provided defaults
// optional, but required for
refetchOnFocus
/refetchOnReconnect
behaviors//setupListeners(store.dispatch)
return store;
}
export const store = makeStore();
When I try to use redux-persist with the above code I am getting an error. I tried multiple things but nothing works , sometimes it is very hard to understand typescript errors
import { Action, ThunkAction, combineSlices, configureStore } from '@reduxjs/toolkit';
import {
persistReducer
} from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import { authSlice } from './slice/auth/authSlice';
import { cartSlice } from './slice/cart/cartSlice';
import { skillSlice } from './slice/skill/skillSlice';
const persistConfig = {
key: 'root',
version: 1,
storage,
}
//
combineSlices
automatically combines the reducers using// their
reducerPath
s, therefore we no longer need to callcombineReducers
.const rootReducer = combineSlices(
authSlice, cartSlice, skillSlice
);
const persistedReducer = persistReducer(persistConfig, rootReducer);
// Infer the
RootState
type from the root reducerexport type RootState = ReturnType;
// The store setup is wrapped in
makeStore
to allow reuse// when setting up tests that need the same store config
export const makeStore = (preloadedState?: Partial) => {
const store = configureStore({
reducer: persistedReducer,
// Adding the api middleware enables caching, invalidation, polling,
// and other useful features of
rtk-query
.// middleware: getDefaultMiddleware => {
// return getDefaultMiddleware().concat(quotesApiSlice.middleware)
// },
preloadedState
})
// configure listeners using the provided defaults
// optional, but required for
refetchOnFocus
/refetchOnReconnect
behaviors//setupListeners(store.dispatch)
return store;
}
export const store = makeStore();
// Infer the type of
store
export type AppStore = typeof store;
// Infer the
AppDispatch
type from the store itselfexport type AppDispatch = AppStore["dispatch"];
export type AppThunk<ThunkReturnType = void> = ThunkAction<ThunkReturnType, RootState, unknown, Action>;
Beta Was this translation helpful? Give feedback.
All reactions