我正在尝试在 redux 中插入项目,但是当我单击按钮时出现以下错误:
TypeError: state.reduxCart is not iterable
我的减速器代码:
const INITIAL_STATE = {
reduxCart: [],
reduxCartCounter: 0
}
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case 'ADDITEM':
return {
...state,
reduxCart: [action.payload , ...state.reduxCart] // ERROR
}
case 'ADDCOUNTER':
return ({
...state,
reduxCartCounter: action.payload
})
default:
return state;
}
}
这是我的商店代码:
import rootReducer from './reducer';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
const persistConfig = {
key: 'root',
storage,
}
const persistedReducer = persistReducer(persistConfig, rootReducer);
const store = createStore(persistedReducer, applyMiddleware(thunk));
const persistor = persistStore(store);
export { store, persistor };
我的行动代码:
export function AddToReduxCart(item) {
return dispatch => {
dispatch({ type: 'ADDITEM', payload: item })
}
}
如何解决这个问题?任何帮助,将不胜感激。