Search code examples
reduxexpo

getting Error "The slice reducer for key "weathReducer" returned undefined during intialization in my expo app"


running into Error: The slice reducer for key "weatherReducer" 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.

at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError
at node_modules/metro-runtime/src/polyfills/require.js:204:6 in guardedLoadModule
at http://192.168.0.12:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:146332:3 in global code

Solution

  • In general when writing a reducer function, there is an initialization call which invokes the reducer without an action argument. If you're in that call, you must return initialState.

    var initialState = {
      temperature: undefined,
      units: 'celsius',
      isLoading: false,
    }
    
    function weatherReducer(state, action) {
      switch(action) {
        case 'SET_TEMPERATURE': 
          return { ...state, temperature: action.temperature }
        // ... 
        default: 
          return state || initialState // <- here
      }
    }