Search code examples
iosreact-nativereact-reduxcreate-react-native-app

React Native: Yet another "Undefined is not an object (evaluating action.type)


I'm developing a CRNA application, however, the store connection is not working and I'm receiving the above error when creating the store.

"Undefined is not an object (evaluating action.type)

Searching for similar problems, I got to this question, which is a reducer being called while passed to the createStore function, which is not my case.

And this one, which is related to AnalyticsTracker called before an async dispatcher, also not my case.

Here's the minimum code to reproduce.

App.js

import React from 'react';
import {
  View,
  Text
} from 'react-native'; 
import { Provider } from 'react-redux';
import store from './store';

class App extends React.Component {

  render() {
    return (
      <Provider store={store}>
         <View>
            <Text>Hello</Text>
         </View>
      </Provider>
    );
  }
}

store.js

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

import reducer from './reducer';
// Here the error happens
export default createStore(reducer, applyMiddleware(thunk));

reducer.js

import actionTypes from './action_types';
const initialState = {
}

export default (action, state=initialState) => {
    // This is the top line on stacktrace
    switch (action.type) {
        case actionTypes.MY_ACTION:
            return state;
    }
    return state;
}

I have tried a few changes in my code, i.e: removing the middlewares.

Any idea why is it happening? Am I missing something?


Solution

  • I have noticed that your createStore call is false, since enhancers are passed as the third parameter. Change it to:

    const store = createStore(persistedReducer, undefined, applyMiddleware(thunk));
    

    Additionally the structure of your reducer is false. Your first parameter in your reducer should be the initialState followed by the action as the second parameter - this is why you get undefined is not an object!.

    As described in Reducers, it has to have a signature of (previousState, action) => newState, is known as a reducer function, and must be pure and predictable.

    From: https://redux.js.org/recipes/structuringreducers