I have configured redux-persist with a traditional react-redux setup like this:
onst persistConfig = {
key: 'root',
storage,
whitelist: ['todos'],
};
const persistedReducer = persistReducer(persistConfig, reducer);
const store = createStore(
persistedReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
const persistor = persistStore(store);
// wrapper
const StateProvider = ({ children }) => {
return (
<Provider store={store}>
<PersistGate loading={<div>Loading...</div>} persistor={persistor}>
{children}
</PersistGate>
</Provider>
);
};
But, how can I configure it with redux-toolkit? So far I have tried this:
const persistedReducer = persistReducer(persistConfig, todoreducer);
const store = configureStore({
reducer: {
todos: persistedReducer,
},
});
const persistor = persistStore(store);
// wrapper
const StateProvider = ({ children }) => {
return (
<Provider store={store}>
<PersistGate loading={<div>Loading...</div>} persistor={persistor}>
{children}
</PersistGate>
</Provider>
);
};
But, It is not working. I can't get the todos
by const todos = useSelector(state => state.todos);
it returns undefined.