Reducer returned undefined during initialization but am returning initialState as default in reducer switch

Viewed 4317

I am attempting to follow the instructions found in the redux-persist docs to delay the render until rehydration is complete, and I am now receiving this error

"Reducer 'auth' 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."

However, I am defining and returning the initialState in my auth reducer as follows:

const initialState = {
    authenticated: false,
    authenticating: false,
    loginError: '',
}
export default function reducer(state = initialState, action) {
    switch (action.type) {
        ...my switch cases here

        default:
        return state
    }
}

I did not change how I am creating my store so I am not sure why I am now receiving this error. I will also include my store creation and combine reducers method below

const store = createStore(
    reducers,
    undefined,
    compose(
        applyMiddleware(thunk),
        autoRehydrate({ log: true }),
        window.devToolsExtension ? window.devToolsExtension() : (f) => f
    )
)

export default class AppProvider extends Component {
  constructor(props) {
    super(props)
    this.state = {
      rehydrated: false,
    }
  }

  componentWillMount() {
    persistStore(store, {storage: AsyncStorage}, () => {
      this.setState({ rehydrated: true })
    })
  }

  render() {
    if (!this.state.rehydrated) {
      return <ActivityIndicator />
    }
    return (
      <Provider store={store}>
      <App/>
      </Provider>
    )
  }
}

My reducers export file

export default reducers = combineReducers({
    auth,
    connection,
    initialLoad,
    queue,
})
2 Answers
Related