React Navigation on tab change

后端 未结 5 1239
清歌不尽
清歌不尽 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:51

    If you are using react-navigation version 5, then you can add tab-press listener right in your tab screen definition, to do some pre-processing, as mentioned in docs

    
         {
                if (userIsNotLoggedIn) {
                  // Prevent default action
                  e.preventDefault();
                  navigation.navigate("LoginScreen");
                }
              },
            }}
          />
          
    
    

    Explanation: When Home tab will be clicked in BottomBar, and if user is not logged in, the HomeScreen won't open and instead LoginScreen will open(Note: LoginScreen is navigation name which will be registered some where with a screen). But if user is logged in, then it will behave normally.

提交回复
热议问题