react-navigation / redux-helpers

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

SwitchNavigator doesn't work with Redux integration outlined in guide #18

Closed kylegillen closed 6 years ago

kylegillen commented 6 years ago

Hi.

I'm using the exact Redux Integration outlined in the guide found here: https://reactnavigation.org/docs/redux-integration.html

However, when I export a SwitchNavigator (with a StackNavigator as its initial route) as my default Navigator I'm met with the following error:

undefined is not an object (evaluating 'prevState.index')

Here is a Snack reproducing the bug: https://snack.expo.io/Hy643mQtz

Image of stack trace

Any help is much appreciated.

andreaPortfolio commented 6 years ago

I have the same problem... Any news?

mattvick commented 6 years ago

I have the same problem too... Any news?

mattvick commented 6 years ago

@nextriot and @andreaPortfolio what version of react-navigation and react-navigation-redux-helpers are you using?

The react-navigation ReduxExample, which runs, uses the following versions:

react-navigation: 2.0.0-alpha.5 react-navigation-redux-helpers: ^1.0.0

andreaPortfolio commented 6 years ago

I'm using: "react-native": "0.52.0", "react-navigation": "^1.5.1", "react-navigation-redux-helpers": "^1.0.3"

mattvick commented 6 years ago

@nextriot and @andreaPortfolio do you have nested navigators, and is the SwitchNavigator the root navigator?

A quick fix is to set initialNavState to null here: ReduxExample/src/reducers/index.js#L10

kylegillen commented 6 years ago

@mattvick, thanks for helping.

Please check the snack (https://snack.expo.io/Hy643mQtz) for any further questions.

The SwitchNavigator is the root, and it has nested StackNavigators.

"react-navigation": "^1.5.1". "react-navigation-redux-helpers": "^1.0.3".

I don't think 2.0.0-alpha.5 is publicly available. So I don't think that's the issue.

mattvick commented 6 years ago

@nextriot take a look at https://snack.expo.io/ByL0C9YFz it runs after setting const initialState = null. Can anyone explain what the side effects of setting the initial state to null might be?

andreaPortfolio commented 6 years ago

I changed my reducer initial state from:

const initialState = AppNavigator.router.getStateForAction( AppNavigator.router.getActionForPathAndParams("AuthLoading") ); to

const initialState = null;

and now it works!!!

Thank you @mattvick

mattvick commented 6 years ago

No worries @andreaPortfolio. Glad I could help.

I've read several blog posts on the subject, but would be great if there was some clear documentation outlining how to set initialState for nested navigators.

Also I'd like to know why the navigation state has to be set in the app and as initialState, which seems like setting the same thing in two places.

pedrobertao commented 6 years ago

This need some clarification. Just setting initialState to null as a solution kinda bugs my mind.

shelldandy commented 6 years ago

Install react-navigation@next to fix the issue :D

Ashoat commented 6 years ago

Is anybody still experiencing this issue?

shelldandy commented 6 years ago

Yes but i installed react-navigation@next and the issue was fixed in there

Ashoat commented 6 years ago

I think react-navigation-redux-helpers@1.0.6 should fix this issue for people who are still on react-navigation@1.x. If anybody is still experiencing this issue let me know!