Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unmount inactive screens in bottom tab navigator react-navigation?

Im using react-navigation for react-native. Is there an option to make that inactive tab screens get unmounted like unmountInactiveRoutes: true in DrawerNavigator?? I cant find something like unmountInactiveRoutes for BottomTabNavigator.

I have two stacknavigators inside a BottomTabNavigator and I want to unmount them automatically.

  • BottomTabNavigator
    • Stack1
      • Screen
      • Screen
    • Stack2
      • Screen
      • Screen
like image 323
Braven Avatar asked Nov 04 '19 15:11

Braven


Video Answer


4 Answers

You can unmount screens in bottom tab by adding option in navigation screenOptions (or in Tab.Navigator screenOptions):

unmountOnBlur: true

You can do it in Tab & Drawer Navigations but not in Stack Navigation.

And you can also add unmount individual screen by adding same option in Tab or Drawer Screen option.

like image 130
Ubaid Ullah Avatar answered Oct 18 '22 22:10

Ubaid Ullah


So I don't know if you can unmount components that are inactive personally I did not find it however this is my workaround withNavigationFocus(FocusStateLabel) and if isFocused is false. returning null. So this will give you more or less what you are looking for. If isFocused is true, you'll render what you usually render. If false you'll return null. resulting in the unmounting of your components

Some reference https://reactnavigation.org/docs/en/with-navigation-focus.html

like image 6
jstuartmilne Avatar answered Oct 18 '22 22:10

jstuartmilne


I tried Ubaid’s answer it works. But you can try this one too: Use

import {useIsFocused} from '@react-navigation/native';
const isFocused = useIsFocused();
useEffect(() => {
    // Do whatever you want to do when screen gets in focus
  }, [props, isFocused]);

It works perfectly fine.

like image 6
Muhammad Noman Avatar answered Oct 18 '22 23:10

Muhammad Noman


I found two way unmount.

  1. First method is just trigger the unmount using useFocusEffect. With my experience this is not completely unmount component. It just trigger only unmount function to unsubscribe events. https://reactnavigation.org/docs/function-after-focusing-screen/#triggering-an-action-with-a-focus-event-listener

  2. Second method is completely unmount component when the navigating. This one is working as react unmount. https://reactnavigation.org/docs/bottom-tab-navigator/#unmountonblur

like image 1
suresh herath Avatar answered Oct 19 '22 00:10

suresh herath