化简器"language"初始化期间返回未定义 - 组合错误化简器



我对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/rereducer/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.

您的减速器缺少默认的开关情况。至于返回语法,您可以直接返回action.payload。返回的值在reducer下次运行时作为state传递给reducer。

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
}
}

最新更新