开发者问题收集

我的反应应用程序给出“动作未定义”的错误

2018-09-09
218

每当我启动 React 应用程序时,它都会给出未定义操作的错误。我在 switch(action.type) 中的 Reducer 中使用了该操作,并且遇到了我搜索过的问题,但语法相同,或者也许我在其他地方犯了错误,但我无法弄清楚。

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 组合 Reducers

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,
    )
 }
1个回答

正如评论中所述,您需要反转将 initialStaterootReducer 传递给 createStore 的顺序:即将 ConfigureStore.js 更改为

import rootReducer from "../reducers/index";
import { createStore } from 'redux';

export default function (initialState) {
    return createStore(
        rootReducer,
        initialState,
    )
 }
FK82
2018-09-09