I got an error when using the shortcut-persist. I could find several documents about redux-persist v5. And I just follow the official use case. But I am confused by this. Before I use redux-persist, I can correctly get the state from the store. But I want to save the login state to local. So I try to use redux-persist. Then I had problems. Here is my code:
reducer.js
const initialState = { isLogin: false, uname: "", } const userReducer = (state = initialState, action) => { switch(action.type) { case 'DO_LOGIN': return Object.assign({}, state, { isLogin: true, uname: action.payload.username }) default: return state } } const reducers = combineReducers({ userInfo: userReducer }) export default reducers
store.js
import thunk from 'redux-thunk' import { createLogger } from 'redux-logger' import { createStore, applyMiddleware, compose } from 'redux' import { persistStore, persistCombineReducers } from 'redux-persist' import storage from 'redux-persist/es/storage' import reducers from '../reducers' const loggerMiddleware = createLogger() const middleware = [thunk, loggerMiddleware] const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose const configureStore = composeEnhancers( applyMiddleware(...middleware), )(createStore) const config = { key: 'root', version: 1, storage, } const combinedReducer = persistCombineReducers(config, reducers) const createAppStore = () => { let store = configureStore(combinedReducer) let persistor = persistStore(store) return { persistor, store } } export default createAppStore
App.js
const mapStateToProps = (state) => ({ logged: state.userInfo.isLogin })
When I run this code, I got this TypeError: Cannot read property 'isLogin' of undefined error TypeError: Cannot read property 'isLogin' of undefined And this error message in the Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers. console Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers. Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers.
I think something is wrong when gearboxes are combined. But I have no idea where this is wrong?
source share