Reducer "language" 在初始化期间返回未定义 - combineReducers 出错

Reducer "language" returned undefined during initialization - error with combineReducers

我的 redux 配置有问题,我是 React 和 Redux 的新手,redux 与 vuex 不相似,所以我还不知道如何解决这个问题。 那么让我们进入我的代码。

小提示:我确定我有我需要的每个导入,所以让我只显示代码以便清晰查看。

index.jsx

let store = createStore(reducers);
store.subscribe(() => console.log(store.getState()));
render(
    <Provider store={store}>
        <App />
    </Provider>
, window.document.querySelector('#app-container'));

.../store/reducer/index.js

const reducers = combineReducers({
    language
});

export default reducers;

.../store/reducer/language/languageFrom.js

const languageFromReducer = (state = "", action) => {
    switch(action.type) {
        case "SET_LANGUAGE":
            return state = action.payload; //I'm almost sure that this one is wrong O.o
    }
}

export default languageFromReducer;

.../store/action/languageFrom.js

export const setLanguage = (language) => {
    return {
        type: "SET_LANGUAGE",
        payload: language
    };
};

这是我为 redux 配置的,我只得到关于未定义的初始状态的信息。我一直在寻找关于它的任何讨论,但我没有找到可以清楚地解决我的问题的东西。

整个错误日志:

Uncaught Error: Reducer "language" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.

您的减速器缺少默认开关盒。至于 return 语法,直接 return action.payload 即可。 returned 值在下次运行时作为 state 传递给减速器。

const languageFromReducer = (state = "", action) => {
    switch(action.type) {
        case "SET_LANGUAGE":
            return action.payload; //I'm almost sure that this one is wrong O.o
        default:
            return state
    }
}