Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined is not an object (evaluating 'state.routes[state.index]') #1357

Closed
veedeo opened this issue May 3, 2017 · 11 comments
Closed

undefined is not an object (evaluating 'state.routes[state.index]') #1357

veedeo opened this issue May 3, 2017 · 11 comments

Comments

@veedeo
Copy link

veedeo commented May 3, 2017

Im trying to implement Redux with reactnavigation
this is a sample code:

const initialState = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Main'));
  const navReducer = (state = initialState, action) => {
    console.tron.log({state,action,nextState})
    const nextState = AppNavigator.router.getStateForAction(action, state);

    // Simply return the original `state` if `nextState` is null or undefined.
    return nextState || state;
  };

Current Behavior

undefined is not an object (evaluating 'state.routes[state.index]')
getStateForAction
index.android.bundle?platform=android&dev=true&hot=true&minify=false:78599:44

{
  state:{
    index: 0,
    routes: [ { type: undefined, routeName: 'Main', key: 'Init' } ]
  },
  action: { type: '@@redux/INIT' },
  nextState: undefined
}
software version
react-navigation 1.0.0-beta.9
react-native 0.43.4
@veedeo veedeo closed this as completed May 3, 2017
@threecee
Copy link

@veedeo I'm having the same problem. Since you closed, maybe you figured it out?

@q273255702
Copy link

+1

@crjackso
Copy link

+1 @veedeo, can you provide any insight?

@mattotoole0
Copy link

I'm having this problem as well.

@cityvoice
Copy link

cityvoice commented May 26, 2017

If you use nested navigators integerated with redux, try change the code

const initialState = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Main'));

to

const initialNavState=AppNavigator.router.getStateForAction(NavigationActions.reset({
	index: 0,
	actions: [
	  NavigationActions.navigate({
		routeName: 'Main',
	  }),
	],
}))

It works for me, but I don't know why, below is my nested navigators
AppNavigation.js

import MainScreenNavigator from './MainTab';
import ArticleDetailScreen from '../containers/ArticleDetailScreen';
export const AppNavigator = StackNavigator({
  Main: { screen: MainScreenNavigator},
  ArticleDetail: 
	{
		screen: ArticleDetailScreen,
		navigationOptions: ({navigation}) => ({
			title: `${navigation.state.params.title} detail`,
		})
	},
},{initialRouteName:'Main'});

MainTab.js

const MainTab = TabNavigator({
  Home: {
    screen: MainScreen,
  },
  Discovery: {
    screen: DiscoveryScreen,
  },
  ...
}, 	  
  {
	tabBarPosition :'bottom',
	initialRouteName:'Home',
       ...
  })

hope to help you!

@sunnylqm
Copy link

@cityvoice It works.
This issue only happens if initialNavState is for a TabNavigator. @grabbou @satya164

@xsgkaka
Copy link

xsgkaka commented Jun 2, 2017

@cityvoice
it works.but why

@Selman555
Copy link

@cityvoice
Yes it works!!!
You don't know why? That's fine, I wished I would have been smart enough to come up with your idea because I was clueless

@homewb
Copy link

homewb commented Aug 4, 2017

@cityvoice
Amazing!!
I guess it is because StackNavigator may need to know the initial index for TabNavigator. Just guess. It doesn't matter anyways.

@deot
Copy link

deot commented Sep 20, 2017

@cityvoice
Amazing!!

Thanks!!

@rochapablo
Copy link

rochapablo commented Sep 21, 2017

@cityvoice, for a second I can see the screen but right after I get

There is no route defined for key LoginNav. Must be one of: 'Login'
export const AppNavigator = StackNavigator(
  {
    LoginNav: { screen: LoginNav },
    RegisterNav: { screen: RegisterNav },
    Index: { screen: Drawer }
  },
  {
    headerMode: 'none',
    mode: Platform.OS === 'ios' ? 'modal' : 'card',
    initialRouteName: 'LoginNav'
  }
)
const LoginNav = StackNavigator(
  {
    Login: { screen: Login }
  }, {
    headerMode: 'none',
    mode: Platform.OS === 'ios' ? 'modal' : 'card',
    initialRouteName: 'Login'
  }
)

I tried #2565 (comment) but still the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests