如何修复TypeError:undefined不是对象(评估“ state.routes”)

时间:2019-10-13 18:18:40

标签: javascript reactjs react-native expo redux-persist

我正在关注react导航文档,react-navigation-redux-helpers的文档,但始终会向我抛出此错误。另外,如果我清除了persistGate,错误将得到修复,但是我需要保留一些数据,所以这不是一个选择

这是我的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);

这是我的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);

这是我的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;

这是我的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>
        );
    }
 }

1 个答案:

答案 0 :(得分:1)

我知道为时已晚,但应该可以帮助其他人:))

在您的mystore.js中

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

应如下所示:

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