Home>
  • I want to resolve an error incombineReducers ()
Error message

Error: Reducer"page"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 i don't want to set a value for this reducer, you can use null instead of undefined.

./ src/reducers/index.js
src/reducers/index.js: 21

  

18 |
  19 |
  20 |
  >21 | export default combineReducers ({page,});
  22 |
  23 |
  24 |

index.js
import React from 'react';
import ReactDOM from 'react-dom';
import {createStore} from 'redux';
import {Provider} from 'react-redux';
import reducer from './reducers';
import App from './App';
const store = createStore (reducer);
ReactDOM.render (
  <Provider store = {store}>
    <App />
  </Provider&gt ;,
  document.getElementById ('root')
);
reducers/index.js
import {combineReducers} from 'redux';
import page from './page';
export default combineReducers ({page,})
reducers/page.js
const initialState = {
  value: '',
}
export default (state = initialState, action) =>{
  switch (action.type) {
    case 'GO_TO_ABOUT':
      return {value: 'ABOUT',}
    default:
      return state
  }
}
Corresponding code
function assertReducerShape (reducers) {
  Object.keys (reducers) .forEach (function (key) {
    var reducer = reducers [key];
    var initialState = reducer (undefined, {type: ActionTypes.INIT});
    if (typeof initialState === 'undefined') {
      throw new Error ('Reducer "' + key + '" 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 i don \ 't want to set a value for this reducer,' + 'you can use null instead of undefined.');
    }
    var type = '@@ redux/PROBE_UNKNOWN_ACTION_' + Math.random (). toString (36) .substring (7) .split (''). join ('.');
    if (typeof reducer (undefined, {type: type}) === 'undefined') {
      throw new Error ('Reducer "' + key + '" returned undefined when probed with a random type.' + ('Don \' t try to handle '+ ActionTypes.INIT +' or other actions in "redux/*" ' ) + 'namespace. They are considered private. Instead, you must return the' + 'current state for any unknown actions, unless it is undefined,' + 'in which case you must return the initial state, regardless of the' + ' action type.The initial state may not be undefined, but can be null. ');
    }
  });
}


The firsttypeof initialState ==='undefined'has thrown an exception.
I thought that it was undefined at the import destination, but it seems that there is no problem even if compared with the reference writing.