Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
        );
    }
 }
like image 932
Jean Avatar asked Oct 13 '19 18:10

Jean


People also ask

What is the reason for getting error TypeError undefined is not an object evaluating this Setstate?

The “TypeError: 'undefined' is not an object” error occurs when a property is accessed or a method is called on an undefined object. This error is shown only on safari browser.

How do I reset navigation react native?

To reset the navigation stack for the home screen with React Navigation and React Native, we can use the navigation. dispatch and the CommonActions. reset methods. import { CommonActions } from "@react-navigation/native"; navigation.


1 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,
)
like image 167
Hammad Siraj Avatar answered Oct 13 '22 01:10

Hammad Siraj