使用Redux响应本机导航:react-navigation-redux-helpers-“未定义不是对象(评估state.routes)”

时间:2019-01-13 19:22:58

标签: reactjs react-native redux react-redux react-native-navigation

我正在尝试在react本机应用程序中实现redux和导航,因此我试图遵循react-native-redux-helpers(here)的说明。

但是,尝试启动应用程序时收到以下错误消息:

Error

App.js:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { Provider, connect } from 'react-redux';
import rootReducer from './src/reducers/index';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import { reduxifyNavigator, createReactNavigationReduxMiddleware, createNavigationReducer } from 'react-navigation-redux-helpers';

import LoginScreen from './src/screens/LoginScreen';
import NotesScreen from './src/screens/NotesScreen';

const AppNavigator = createStackNavigator({
  Login: {
        screen: LoginScreen
  },
  Notes: {
        screen: NotesScreen
  }
})

const navReducer = createNavigationReducer(AppNavigator);

const appReducer = combineReducers({
  rootReducer,
  navReducer
})

const middleware = createReactNavigationReduxMiddleware('Login', state => state.nav);

const App = reduxifyNavigator(AppNavigator, 'Login');

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

const AppWithNavigationState = connect(mapStateToProps)(App);

const store = createStore(appReducer, applyMiddleware(middleware));

export default class Root extends React.Component {
  render() {
        return (
              <Provider store={store}>
                    <AppWithNavigationState />
              </Provider>
        )
  }
}

1 个答案:

答案 0 :(得分:0)

请尝试执行文档中所述的代码,甚至包括名称。确实应该可以,并且您的情况如下:


    //delete
import { reduxifyNavigator, ...} from 'react-navigation-redux-helpers';
//add 
import { createReduxContainer } from 'react-navigation-redux-helpers';

const AppNavigator = createStackNavigator({
    Login: {
          screen: LoginScreen
    },
    Notes: {
          screen: NotesScreen
    }
  })

  const navReducer = createNavigationReducer(AppNavigator);

  const appReducer = combineReducers({
    rootReducer,
    nav : navReducer
  })

  const middleware = createReactNavigationReduxMiddleware(state => state.nav);

  const App = createReduxContainer(AppNavigator);

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

  const AppWithNavigationState = connect(mapStateToProps)(App);

  const store = createStore(appReducer, applyMiddleware(middleware));

  export default class Root extends React.Component {
    render() {
          return (
                <Provider store={store}>
                      <AppWithNavigationState />
                </Provider>
          )
    }
  }