我的反应应用程序,当我启动我的反应应用程序时,它会给出一个未定义的操作错误。我在我的reducer中使用了switch(action.type),我得到了我搜索它的问题,但语法是相同的,或者我在其他地方做错了,但我无法弄明白。
Action.js
export function createUser(details)
{
return{ type: 'CREATE_USER',details}
}
Reducer.js
export default function userReducer(state= initialState, action) {
switch (action.type) {
case 'CREATE_USER':
return[...state,
Object.assign({}, action.details)
];
default:
return state;
}
}
index.js形成组合减速器
import {combineReducers} from 'redux';
import userReducer from './Reducers';
const rootReducer = combineReducers({
detail: userReducer
});
export default rootReducer;
ConfigureStore.js
import rootReducer from "../reducers/index";
import {createStore} from 'redux';
export default function (initialState) {
return createStore(
initialState,
rootReducer,
)
}
如评论中所述,您需要颠倒将initialState
和rootReducer
传递给createStore
的顺序:即将您的ConfigureStore.js更改为
import rootReducer from "../reducers/index";
import { createStore } from 'redux';
export default function (initialState) {
return createStore(
rootReducer,
initialState,
)
}