我正在尝试使用react-navigation将StackNavigator构建到我的redux存储中,并且不断出现错误
here(上面标题中已提到)。

以下是我使用redux的react-native项目的代码。该错误在我调用AppNavigator.router的减速器上弹出。

src / AppNavigator.js

import { StackNavigator, createStackNavigator } from 'react-navigation';
import Welcome from './screens/Welcome';
import Dashboard from './screens/Dashboard';
import Login from './screens/Login';

export const AppNavigator = StackNavigator(
  {
    Welcome: {
      screen: Welcome,
      navigationOptions: ({
        header: 'null',
        headerStyle: {
          backgroundColor: 'white',
          borderBottomWidth: 0,
        },
        headerTitleStyle: {
          fontSize: 30,
          fontFamily: 'Roboto-Bold',
        },
      }),
    },
    Dashboard: {
      screen: Dashboard,
      navigationOptions: ({
        title: 'Dashboard',
        headerStyle: {
          backgroundColor: 'white',
          borderBottomWidth: 0,
        },
        headerTitleStyle: {
          fontSize: 30,
          fontFamily: 'Roboto-Bold',
        },
        headerLeft: null,
      }),
    },
    Login: {
      screen: Login,
      navigationOptions: ({
        title: 'Login or Register',
        headerStyle: {
          backgroundColor: 'white',
          borderBottomWidth: 0,
        },
        headerTitleStyle: {
          fontSize: 30,
          fontFamily: 'Roboto-Bold',
        },
        headerLeft: null,
      }),
    },
  },
  {
    initialRouteName: 'Dashboard',
  },
);


src / AppWithInternalState.js

import React from 'react';
import { connect } from 'react-redux';
import { addNavigationHelpers } from 'react-navigation';
import AppNavigator from './AppNavigator';

const AppWithInternalState = ({ dispatch, nav }) => (
  <AppNavigator navigation={
    addNavigationHelpers({
      dispatch,
      state: nav,
    })
  }/>
);

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

export default connect(mapStateToProps)(AppWithInternalState);


/src/reducers/navReducer.js

import React from 'react';
import AppNavigator from '../AppNavigator';

const router = AppNavigator.router;
const mainNavAction = router.getActionforPathandParams('Welcome');
const initialNavState = router.getStateForAction(mainNavAction);

export default navReducer = (state = initialNavState, action) => (
  router.getStateForAction(action, state)
);


感谢任何帮助!

最佳答案

import AppNavigator from '../AppNavigator';-> AppNavigator未定义,因为您没有默认导出。

AppNavigator.js中使用export default AppNavigator = StackNavigator(注意默认值而不是const)。

替代:import { AppNavigator } from '../AppNavigator';中的navReducer.js

10-08 12:15