The Issue
Navigator.getCurrentRoutes() values are not correct in when called from willfocus & didfocus.
I've implemented my own navigation bar (without the Navigator.navigationBar prop)
I wanted to be able to toggle the back button according the current routes stack. (by using Navigator.getCurrentRoutes())
navigator.navigationContext.addListener('willfocus', this._onNavigatorWillFocus),
_onNavigatorWillFocus(navigationEvent) {
const currentRoutes = this.props.navigator.getCurrentRoutes();
const hideBackButton = currentRoutes.length > 1;
},
(any suggestions for a better solution would be welcomed)
I start the navigator with a single initial route.
pushed a new route, and from the 2nd screen I tried both:
- calling
Navigator.pop
- pan gesture to return to the 1st screen
in both cases, when I call getCurrentRoutes from willfocus is called with 2 routes.
same issue happens when using didfocus
after going over the code I found the issue, in _handlePanResponderRelease
https://github.com/facebook/react-native/blob/master/Libraries/CustomComponents/Navigator/Navigator.js#L659-L667
the routes stack is updated in _cleanScenesPastIndex but as you can see _emitWillFocus is called before.
I did noticed that the navigator.state.presentedIndex is the correct value.
which is how it's done in the NavigationBarSample
https://github.com/facebook/react-native/blob/master/Examples/UIExplorer/Navigator/NavigationBarSample.js#L44-L49
and I can also check the index from the routes stack:
_onNavigatorWillFocus(navigationEvent) {
const currentRoutes = this.props.navigator.getCurrentRoutes();
const currentRoute = navigationEvent.data.route;
const presentedIndex = _.indexOf(currentRoutes, currentRoute);
this seems like the wrong way to go about this...
Thoughts?
BTW, I've noticed this has some related issues:
#1252
#1346
The Issue
Navigator.getCurrentRoutes()values are not correct in when called fromwillfocus&didfocus.I've implemented my own navigation bar (without the
Navigator.navigationBarprop)I wanted to be able to toggle the back button according the current routes stack. (by using
Navigator.getCurrentRoutes())(any suggestions for a better solution would be welcomed)
I start the navigator with a single initial route.
pushed a new route, and from the 2nd screen I tried both:
Navigator.popin both cases, when I call
getCurrentRoutesfromwillfocusis called with 2 routes.same issue happens when using
didfocusafter going over the code I found the issue, in
_handlePanResponderReleasehttps://github.com/facebook/react-native/blob/master/Libraries/CustomComponents/Navigator/Navigator.js#L659-L667
the routes stack is updated in
_cleanScenesPastIndexbut as you can see_emitWillFocusis called before.I did noticed that the
navigator.state.presentedIndexis the correct value.which is how it's done in the
NavigationBarSamplehttps://github.com/facebook/react-native/blob/master/Examples/UIExplorer/Navigator/NavigationBarSample.js#L44-L49
and I can also check the index from the routes stack:
this seems like the wrong way to go about this...
Thoughts?
BTW, I've noticed this has some related issues:
#1252
#1346