当我启动我的React应用程序时,它会产生未定义的操作错误。我在我的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,
)
}
答案 0 :(得分:1)
如评论中所述,您需要颠倒将initialState
和rootReducer
传递到createStore
的顺序:即更改您的 ConfigureStore.js 到
import rootReducer from "../reducers/index";
import { createStore } from 'redux';
export default function (initialState) {
return createStore(
rootReducer,
initialState,
)
}