我的React应用显示“操作未定义”错误

时间:2018-09-09 08:30:10

标签: javascript reactjs redux react-redux

当我启动我的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,
    )
 }

1 个答案:

答案 0 :(得分:1)

如评论中所述,您需要颠倒将initialStaterootReducer传递到createStore的顺序:即更改您的 ConfigureStore.js

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

export default function (initialState) {
    return createStore(
        rootReducer,
        initialState,
    )
 }