react-navigation / redux-helpers

Redux middleware and utils for React Navigation
Other
296 stars 43 forks source link

Invariant Violation: App.router must be provided to createNavigationPropConstructor as of react-navigation-redux-helpers@2.0.0 #104

Closed tianleiyiji123 closed 4 years ago

tianleiyiji123 commented 4 years ago

what is wrong about this react-navigation-redux-helpers version: ^4.0.1 react-navigation version: ^4.0.10

image

tianleiyiji123 commented 4 years ago

### below is my code

import {createAppContainer} from 'react-navigation';
// import {createStackNavigator} from 'react-navigation-stack';
import {createBottomTabNavigator} from 'react-navigation-tabs';
import React from 'react';
import {connect} from 'react-redux';
import {
  createReduxContainer,
  createReactNavigationReduxMiddleware,
  createNavigationReducer,
} from 'react-navigation-redux-helpers';
// import AuthLoading from '../screens/AuthLoading';
// import Search from '../screens/Search';
import CustomTabBarBottom from '../components/CustomTabBarBottom';
import Goods from '../screens/Goods';
import Serve from '../screens/Serve';
import Member from '../screens/Member';
import Rank from '../screens/Rank';
// import Detail from '../screens/Detail/index';
// import CommentsDetail from '../screens/Rank/Comments/Detail';
// import DataDetail from '../screens/DataDetail';
// import ServiceDetail from '../screens/ServiceDetail';
// import SecondPage from '../screens/SecondPage';

const Home = createBottomTabNavigator(
  {
    Goods: {
      screen: Goods,
      navigationOptions: {
        title: '商品',
      },
    },
    Serve: {
      screen: Serve,
      navigationOptions: {
        title: '服务',
      },
    },
    Member: {
      screen: Member,
      navigationOptions: {
        title: '会员',
      },
    },
    Rank: {
      screen: Rank,
      navigationOptions: {
        title: '排行榜',
      },
    },
  },
  {
    tabBarComponent: ({navigation}) => (
      <CustomTabBarBottom navigation={navigation} />
    ),
    initialRouteName: 'Goods',
  },
);

export const AppNavigator = createAppContainer(Home);

// export const AppNavigator = createAppContainer(
//   createStackNavigator(
//     {
//       AuthLoading,
//       Home,
//       Detail,
//       CommentsDetail,
//       DataDetail,
//       Search,
//       ServiceDetail,
//       SecondPage,
//     },
//     {},
//   ),
// );

export const navReducer = createNavigationReducer(AppNavigator);

// fix addListener is not function
export const routeMiddleware = createReactNavigationReduxMiddleware(
  'root',
  state => state.nav,
);

const App = createReduxContainer('root', AppNavigator);

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

// AppWithNavigationState.propTypes = {
//   dispatch: PropTypes.func.isRequired,
//   nav: PropTypes.object.isRequired,
// };

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

export default connect(mapStateToProps)(App);
Ashoat commented 4 years ago

You should be passing the root navigator directly to createReduxContainer and not using createAppContainer at all. If you really want the functionality createAppContainer provides (eg. back button handling) you could try to make the app container look like a navigator by hoisting the root navigator’s static onto the app container using hoist-non-react-statics. Then presumably you could pass the app container into createReduxContainer.