React Navigation on tab change

后端 未结 5 1240
清歌不尽
清歌不尽 2021-02-08 12:58

Without using Redux, how do I detect a tab change with a react navigation tab navigator?

I know I need to somehow use onNavigationStateChange but I can\'t figure out

5条回答
  •  别那么骄傲
    2021-02-08 13:36

    export default () =>  { this.nav = ref; }}
        onNavigationStateChange={(prevState, currentState) => {
           const getCurrentRouteName = (navigationState) => {
             if (!navigationState) return null;
             const route = navigationState.routes[navigationState.index];
             if (route.routes) return getCurrentRouteName(route);
             return route.routeName;
           };
        global.currentRoute = getCurrentRouteName(currentState);
      }}
    />;
    

    If you don't want to use redux, this is how you can store global information about the current route, so you can both detect a tab change and also tell which tab is now active.

提交回复
热议问题