How to fix TypeError: undefined is not an object (evaluating 'state.routes')

Viewed 5022

I'm following the react navigation documentation, react-navigation-redux-helpers's documentation, but always throw me this error. also if i erase the persistGate the error gets fixed but i need to persist some data so that shouldn't be an option

This is my store.js

import { createStore, applyMiddleware } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import { AsyncStorage } from 'react-native';
import { createReactNavigationReduxMiddleware } from 'react-navigation-redux-helpers';

import Reducer from './reducers/index';

const persistConfig = {
    key: 'root',
    storage: AsyncStorage,
    blackList: [],
};

const AppReducer = persistReducer(persistConfig, Reducer);

const middleware = createReactNavigationReduxMiddleware(
    (state) => state.navigation,
);

export const store = createStore(AppReducer, applyMiddleware(middleware));
export const persistor = persistStore(store);

This is my app-with-state.js

import AppNavigator from './AppNavigator';

const AppNavigatorWithState = createReduxContainer(AppNavigator);

class ReduxNavigation extends React.Component {
    render() {
        const { state, dispatch } = this.props;

        return <AppNavigatorWithState navigation={state} dispatch={dispatch} />;
    }
}

const mapStateToProps = (state) => ({
    state: state.navigation,
});

export default connect(mapStateToProps)(ReduxNavigation);

this is my AppNavigator.js

const Main = createStackNavigator(
    {
        Home: Home,
        Movie: Movie,
        Category: Category,
    },
    {
        defaultNavigationOptions: {
            header: Header,
        },
    },
);


const TabNavigator = createBottomTabNavigator(
{
        Home: {
            screen: Main,
            navigationOptions: {
                tabBarIcon: <Icon icon='' />,
            },
        },
        About: { screen: About },
        Lucky: { screen: Lucky },
        Profile: { screen: Profile },
    },
    {
        tabBarOptions: {
           activeTintColor: 'white',
           activeBackgroundColor: '#65a721',
        },
    },
);

//const App = createAppContainer(TabNavigator);

export default TabNavigator;

and here is my App.js

import React from 'react';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';

import { store, persistor } from './store';
import Loader from './src/sections/components/loader';
import ReduxNavigation from './src/app-navigator-with-state';
import { createAppContainer } from 'react-navigation';

const Navigation = createAppContainer(ReduxNavigation);

type Props = {};
  export default class App extends React.Component<Props> {
    render() {
        return (
            <Provider store={store}>
                <PersistGate persistor={persistor} loading={<Loader />}>
                    <Navigation />
                </PersistGate>
            </Provider>
        );
    }
 }
2 Answers

I know its too late but it should help others :))

In your mystore.js

const middleware = createReactNavigationReduxMiddleware(
    (state) => state.navigation,
)

should be look like this:

const middleware = createReactNavigationReduxMiddleware(
    (state) => state.router,
)

I also got this error and I didn't find any clue, and stuck. After several hours of debugging, it was coz of my stupid mistake.
This was my Wrong Case:

const customContent = ({props}) => { }

Acutally, props doesn't need {}. Just call (props) like and error was fixed.
This is Right Case:

const customContent = (props) => { }

Please Don't add {} when you call props.
Hope it helps for someone who got error like me.

Related