Skip to content

[Navigator] getCurrentRoutes & willfocus/didfocus Events #2071

@yelled3

Description

@yelled3

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions